overflow.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_overflow) {
  3. unsigned nlextents;
  4. size_t mib[4];
  5. size_t sz, miblen, max_size_class;
  6. void *p;
  7. sz = sizeof(unsigned);
  8. assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
  9. 0), 0, "Unexpected mallctl() error");
  10. miblen = sizeof(mib) / sizeof(size_t);
  11. assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
  12. "Unexpected mallctlnametomib() error");
  13. mib[2] = nlextents - 1;
  14. sz = sizeof(size_t);
  15. assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
  16. NULL, 0), 0, "Unexpected mallctlbymib() error");
  17. assert_ptr_null(malloc(max_size_class + 1),
  18. "Expected OOM due to over-sized allocation request");
  19. assert_ptr_null(malloc(SIZE_T_MAX),
  20. "Expected OOM due to over-sized allocation request");
  21. assert_ptr_null(calloc(1, max_size_class + 1),
  22. "Expected OOM due to over-sized allocation request");
  23. assert_ptr_null(calloc(1, SIZE_T_MAX),
  24. "Expected OOM due to over-sized allocation request");
  25. p = malloc(1);
  26. assert_ptr_not_null(p, "Unexpected malloc() OOM");
  27. assert_ptr_null(realloc(p, max_size_class + 1),
  28. "Expected OOM due to over-sized allocation request");
  29. assert_ptr_null(realloc(p, SIZE_T_MAX),
  30. "Expected OOM due to over-sized allocation request");
  31. free(p);
  32. }
  33. TEST_END
  34. int
  35. main(void) {
  36. return test(
  37. test_overflow);
  38. }