Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created September 5, 2014 11:53
Show Gist options
  • Save 17twenty/49629fd543c86b635599 to your computer and use it in GitHub Desktop.
Save 17twenty/49629fd543c86b635599 to your computer and use it in GitHub Desktop.
Urgh, C strings suck
#include <string.h>
#include <stdio.h>
void printCanary(char * first, char * second)
{
printf("Canaries[%c%c] [%c%c]\n", first[0], first[1], second[0], second[1]);
memset(first, 'X', 2 * sizeof(*first));
memset(first, 'Z', 2 * sizeof(*second));
}
int main(void)
{
char padding[20]; // This is to prevent strcpy blowing away the stack!
char canaryA[2] = { 'X', 'X' };
char destination[5];
char canaryB[2] = { 'Z', 'Z' };
char * source = "STRINGS";
printCanary(canaryA, canaryB);
strcpy(destination, source);
printf("Destination = '%s'\n", destination);
printCanary(canaryA, canaryB);
strncpy(destination, source, sizeof(destination));
printf("Destination = '%s'\n", destination);
printCanary(canaryA, canaryB);
snprintf(destination, sizeof(destination), "%s",source);
printf("Destination = '%s'\n", destination);
printCanary(canaryA, canaryB);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment