tst-reallocarray.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Test for reallocarray.
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include <support/check.h>
  19. #include <libc-diag.h>
  20. #include "tst-malloc-aux.h"
  21. static void *
  22. reallocarray_nowarn (void *ptr, size_t nmemb, size_t size)
  23. {
  24. #if __GNUC_PREREQ (7, 0)
  25. /* GCC 7 warns about too-large allocations; here we want to test
  26. that they fail. */
  27. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  28. #endif
  29. void *ret = reallocarray (ptr, nmemb, size);
  30. #if __GNUC_PREREQ (7, 0)
  31. DIAG_POP_NEEDS_COMMENT;
  32. #endif
  33. return ret;
  34. }
  35. static int
  36. do_test (void)
  37. {
  38. void *ptr = NULL;
  39. void *ptr2 = NULL;
  40. unsigned char *c;
  41. size_t i;
  42. int ok;
  43. const size_t max = ~(size_t)0;
  44. size_t a, b;
  45. /* Test overflow detection. */
  46. errno = 0;
  47. ptr = reallocarray_nowarn (NULL, max, 2);
  48. TEST_VERIFY (!ptr);
  49. TEST_VERIFY (errno == ENOMEM);
  50. errno = 0;
  51. ptr = reallocarray_nowarn (NULL, 2, max);
  52. TEST_VERIFY (!ptr);
  53. TEST_VERIFY (errno == ENOMEM);
  54. a = 65537;
  55. b = max/65537 + 1;
  56. errno = 0;
  57. ptr = reallocarray_nowarn (NULL, a, b);
  58. TEST_VERIFY (!ptr);
  59. TEST_VERIFY (errno == ENOMEM);
  60. errno = 0;
  61. ptr = reallocarray_nowarn (NULL, b, a);
  62. TEST_VERIFY (!ptr);
  63. TEST_VERIFY (errno == ENOMEM);
  64. /* Test realloc-like behavior. */
  65. /* Allocate memory like malloc. */
  66. ptr = reallocarray (NULL, 10, 2);
  67. TEST_VERIFY_EXIT (ptr);
  68. TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 10*2);
  69. memset (ptr, 0xAF, 10*2);
  70. /* Enlarge buffer. */
  71. ptr2 = reallocarray (ptr, 20, 2);
  72. TEST_VERIFY (ptr2);
  73. if (ptr2)
  74. ptr = ptr2;
  75. TEST_VERIFY (malloc_usable_size (ptr) >= 20*2);
  76. c = ptr;
  77. ok = 1;
  78. for (i = 0; i < 10*2; ++i)
  79. {
  80. if (c[i] != 0xAF)
  81. ok = 0;
  82. }
  83. TEST_VERIFY (ok);
  84. /* Decrease buffer size. */
  85. ptr2 = reallocarray (ptr, 5, 3);
  86. TEST_VERIFY (ptr2);
  87. if (ptr2)
  88. ptr = ptr2;
  89. TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 5*3);
  90. c = ptr;
  91. ok = 1;
  92. for (i = 0; i < 5*3; ++i)
  93. {
  94. if (c[i] != 0xAF)
  95. ok = 0;
  96. }
  97. TEST_VERIFY (ok);
  98. /* Overflow should leave buffer untouched. */
  99. errno = 0;
  100. ptr2 = reallocarray_nowarn (ptr, 2, ~(size_t)0);
  101. TEST_VERIFY (!ptr2);
  102. TEST_VERIFY (errno == ENOMEM);
  103. c = ptr;
  104. ok = 1;
  105. for (i = 0; i < 5*3; ++i)
  106. {
  107. if (c[i] != 0xAF)
  108. ok = 0;
  109. }
  110. TEST_VERIFY (ok);
  111. free (ptr);
  112. return 0;
  113. }
  114. #include <support/test-driver.c>