Advertisement
Guest User

Untitled

a guest
Aug 19th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. const unsigned char input[] = {
  6.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  11.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19.     0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x80, 0x00, 0x00,
  20.     0x00,
  21. };
  22.  
  23. static unsigned char *buf1 = NULL;
  24. static unsigned char *buf2 = NULL;
  25.  
  26. static int test(void)
  27. {
  28.     free(buf1);
  29.     buf1 = malloc(sizeof(input));
  30.     if (!buf1) { printf("malloc() failure\n"); exit(1); }
  31.     memcpy(buf1, input, sizeof(input));
  32.  
  33.     free(buf2);
  34.     buf2 = malloc(sizeof(input));
  35.     if (!buf2) { printf("malloc() failure\n"); exit(1); }
  36.     memcpy(buf2, input, sizeof(input));
  37.  
  38.     if (memcmp(buf1, buf2, sizeof(input)) != 0) {
  39.         printf("MEMORY CORRUPTION???\n\n");
  40.         printf("ORIG MEMORY:");
  41.         for (size_t i = 0; i < sizeof(input); i++) {
  42.             if (i % 24 == 0) printf("\n");
  43.             printf("%02hhx ", buf1[i]);
  44.         }
  45.  
  46.         printf("\n\nNEW MEMORY (different bytes suffixed with !):");
  47.         for (size_t i = 0; i < sizeof(input); i++) {
  48.             if (i % 24 == 0) printf("\n");
  49.             printf("%02hhx%c", buf2[i], buf2[i] == buf1[i] ? ' ' : '!');
  50.         }
  51.  
  52.         printf("\n\nCORRUPTED ADDRESS: old=%p+165{%02hhx} new=%p+165{%02hhx}\n\n",
  53.             buf1, buf1[165], buf2, buf2[165]);
  54.  
  55.         return 0;
  56.     }
  57.  
  58.     return 1;
  59. }
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.     int runs = 0;
  64.     int ok = 1;
  65.  
  66.     for (;;) {
  67.         runs++;
  68.  
  69.         ok = test();
  70.  
  71.         if (!ok || runs >= 1000000) {
  72.             printf("%s after %d runs\n",
  73.                 ok ? "Completed" : "Failure",
  74.                 runs);
  75.  
  76.             break;
  77.         }
  78.     }
  79.  
  80.     return !ok;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement