1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// BUFFER OVERFLOW -- but not malicious
void get_input(char* s) {
// pretend this function got arbitrary input from the user.
char hard_coded[] = "abcdefghijk"; // 12 bytes
strcpy(s, hard_coded);
}
int main(int argc, char* argv[]) {
char s[10]; // DON'T DO THIS IN REAL CODE
// This will write too much data -- intentional buffer overflow
get_input(&s[0]); // could have just written get_input(s)
printf("strlen(s) == %zd (not counting '\\0')\n", strlen(s));
printf("s == \"%s\"\n", s);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2019 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.