Skip to content

Instantly share code, notes, and snippets.

@danish-rehman
Created June 3, 2015 06:09
Show Gist options
  • Save danish-rehman/0cb490d52cb37fdf37e3 to your computer and use it in GitHub Desktop.
Save danish-rehman/0cb490d52cb37fdf37e3 to your computer and use it in GitHub Desktop.
strncat bound check
#include "stdio.h"
#define MAXBUF 10
int main(int argc, char **argv)
{
char buf[MAXBUF]; // enough room for 9 characters and one '\0'
strcpy(buf,"hello "); // use 6 characters
strncat(buf, "world!\n", 3);// Strncat() wants the maximum number
// of characters it can copy (not counting the
// terminating null). In this case, there are 3
// free characters left in buf.
// The length passed to strncat() would
// typically be equivalent to
// MAXBUF-1-strlen(buf).
printf("%s\n", buf)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment