madvice

buffer-overflow.c

Dec 25th, 2025 (edited)
156
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5.  
  6. #define BUFFER_SIZE 32
  7.  
  8.  
  9. void signal_handler_seg_fault(int sig) {
  10.     printf("Segmentation fault, accessing memory in page not mapped by process pid %d\n", getpid());
  11.     exit(1);
  12. }
  13.  
  14. int main()
  15. {
  16.     int a = 0;
  17.     char buffer[BUFFER_SIZE];
  18.     printf("Difference: %ld bytes\n", (char*)&a - buffer);
  19.     signal(SIGSEGV, signal_handler_seg_fault);
  20.     gets(buffer);
  21.     printf("Data: %s\n", buffer);
  22.     printf("Value of a: %d\n", a);
  23.     return 0;
  24. }
  25.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment