Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If len is equal to sizeof(buf), the null terminator will be written one byte past the end of buf.

Code Block
bgColor#FFcccc
enum { BUFFERSIZE = 256 };
char buf[256BUFFERSIZE];
ssize_t len = readlink("/usr/bin/perl", buf, sizeof(buf));
buf[len] = '\0';

...

This example ensures there will be no overflow by only reading in sizeof(buf)-1 characters. It also properly checks to see if an error has occurred.

Code Block
bgColor#ccccff

enum { BUFFERSIZE = 256 };
char buf[256BUFFERSIZE];
ssizetssize_t len;

if ((len = readlink("/usr/bin/perl", buf, sizeof(buf)-1)) != -1)
    buf[len] = '\0';
else {
   /* handle error condition */
}

...