malloc-standard-alignment.c 848 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* exercise a bug found in malloc-standard when alignment
  2. * values are out of whack and cause a small overflow into
  3. * actual user data.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #define ok(p) ((void*)p > (void*)0x1000)
  9. #define x \
  10. do { \
  11. printf("%i: phead = %p, phead->link @ %p = %p %s\n", \
  12. __LINE__, phead, \
  13. ok(phead) ? &phead->link : 0, \
  14. ok(phead) ? phead->link : 0, \
  15. ok(phead) ? phead->link == 0 ? "" : "!!!!!!!!!!!" : ""); \
  16. if (phead->link != NULL) exit(1); \
  17. } while (0);
  18. struct llist_s {
  19. void *data;
  20. struct llist_s *link;
  21. } *phead;
  22. int main(int argc, char *argv[])
  23. {
  24. char *line, *reg;
  25. setbuf(stdout, NULL);
  26. setbuf(stderr, NULL);
  27. phead = malloc(sizeof(*phead));
  28. phead->link = NULL;
  29. x line = malloc(80);
  30. x line = realloc(line, 2);
  31. x reg = malloc(32);
  32. x free(line);
  33. x return 0;
  34. }