Skip to content

Instantly share code, notes, and snippets.

@505e06b2
Created October 21, 2020 20:03
Show Gist options
  • Save 505e06b2/f4a7835c58a70e61e233987411636d5b to your computer and use it in GitHub Desktop.
Save 505e06b2/f4a7835c58a70e61e233987411636d5b to your computer and use it in GitHub Desktop.
Check for buffer overflow
#include <stdio.h>
int main() {
struct {
char buffer[sizeof(int)]; //must be word aligned or will be forced by the compiler
volatile int modified;
} container;
container.modified = 0;
printf("System word size is: %lu\nEnter Text: ", sizeof(int));
gets(container.buffer);
if(container.modified) {
printf("Buffer overflow detected\n");
} else {
printf("You didn't overflow\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment