Guest User

Double free 2

a guest
Aug 27th, 2022
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | Cryptocurrency | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct s{
  5.  
  6. int num;
  7.  
  8. };
  9.  
  10.  
  11. int main(){
  12.  
  13. struct s *a,*b,*c,*d,*e,*f;
  14.  
  15. a =  malloc(sizeof(struct s));
  16. printf("a is at %p \n",a);
  17.  
  18. b =  malloc(sizeof(struct s));
  19. printf("b is at %p \n",b);
  20.  
  21. c =  malloc(sizeof(struct s));
  22. printf("c is at %p \n",c);
  23.  
  24.  
  25. free(a);
  26. printf("Freeing a : %p \n",a);
  27.  
  28. free(c);
  29. printf("Freeing c : %p \n",c);
  30.  
  31. free(b);
  32. printf("Freeing b : %p \n",b);  //Bypassing top check
  33.  
  34. free(c);
  35. printf("Freeing c : %p again \n",c); //Double free
  36.  
  37. printf("The freed bins will be added to the corresponding bins \n");
  38. printf("Now we are again allocating memory using malloc into three pointers d,e and f \n");
  39. printf("As they are of the same sizes as the previously allocated chunks, pointer d,e and f will get allocated the chunks that have been just free()d and added to the fastbin earlier \n ");
  40. d = malloc(sizeof(struct s));
  41. printf("d is at %p \n",d);
  42.  
  43. e = malloc(sizeof(struct s));
  44. printf("e is at %p \n",e);
  45.  
  46. f = malloc(sizeof(struct s));
  47. printf("f is at %p \n",f);
  48.  
  49. d->num = 1111;
  50. printf("d->num is assigned to 1111 \n");
  51.  
  52. f->num = 2222;
  53. printf("f->num is assigned to 2222 \n");
  54.  
  55. printf("Now let's print the values of the both e->num and d->num \n");
  56.  
  57.  
  58. printf("d->num is %d \n ",d->num);
  59. printf("f->num is %d \n ",f->num);
  60.  
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment