Advertisement
Chuso

Overflow examples

Oct 22nd, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char** argv)
  6. {
  7.     unsigned short int test_int = 1234;
  8.     char *test_str = "abcd";
  9.  
  10.  
  11.     struct
  12.     {
  13.         char overflooded[3];
  14.         unsigned short int overwritten;
  15.     } overflow_size;
  16.     memset(&overflow_size, 0, sizeof(overflow_size));
  17.  
  18.     overflow_size.overwritten = test_int;
  19.     strcpy(overflow_size.overflooded , test_str);
  20.  
  21.     printf("We should get: %s, %hu\n", test_str, test_int);
  22.     printf("We get:        %s, %hu\n", overflow_size.overflooded, overflow_size.overwritten);
  23.  
  24.  
  25.     struct
  26.     {
  27.         unsigned short int overflooded;
  28.         char overwritten[4];
  29.     } overflow_shift;
  30.     memset(&overflow_shift, 0, sizeof(overflow_shift));
  31.    
  32.     unsigned short int *p_overflooded = &(overflow_shift.overflooded);
  33.     p_overflooded++;
  34.    
  35.     strcpy(overflow_shift.overwritten, test_str);
  36.     (*p_overflooded) = test_int;
  37.  
  38.     printf("We should get: %hu, %hu\n", test_int, test_int);
  39.     printf("We get:        %hu, %s\n", overflow_shift.overflooded, overflow_shift.overwritten);
  40.  
  41.  
  42.     int i;
  43.     struct
  44.     {
  45.         char * overflooded[2];
  46.         unsigned short int overwritten[2];
  47.     } overflow_bounds;
  48.     memset(&overflow_bounds, 0, sizeof(overflow_bounds));
  49.    
  50.     for (i=0; i<=2; i++)
  51.         overflow_bounds.overwritten[i] = test_int;
  52.  
  53.     for (i=0; i<=2; i++)
  54.         overflow_bounds.overflooded[i] = test_str;
  55.  
  56.     printf("We should get:\n");
  57.     for (i=0; i<=2; i++)
  58.         printf("  %s, %hu\n", test_str, test_int);
  59.    
  60.     printf("We get:\n");
  61.     for (i=0; i<=2; i++)
  62.         printf("  %s, %hu\n", overflow_bounds.overflooded[i], overflow_bounds.overwritten[i]);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement