tst-realloc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2013-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <malloc.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <libc-diag.h>
  20. #include <support/check.h>
  21. #include "tst-malloc-aux.h"
  22. static int
  23. do_test (void)
  24. {
  25. void *p;
  26. unsigned char *c;
  27. int save, i, ok;
  28. errno = 0;
  29. /* realloc (NULL, ...) behaves similarly to malloc (C89). */
  30. DIAG_PUSH_NEEDS_COMMENT;
  31. #if __GNUC_PREREQ (7, 0)
  32. /* GCC 7 warns about too-large allocations; here we want to test
  33. that they fail. */
  34. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  35. #endif
  36. p = realloc (NULL, -1);
  37. DIAG_POP_NEEDS_COMMENT;
  38. save = errno;
  39. if (p != NULL)
  40. FAIL_EXIT1 ("realloc (NULL, -1) succeeded.");
  41. /* errno should be set to ENOMEM on failure (POSIX). */
  42. if (p == NULL && save != ENOMEM)
  43. FAIL_EXIT1 ("errno is not set correctly");
  44. errno = 0;
  45. /* realloc (NULL, ...) behaves similarly to malloc (C89). */
  46. p = realloc (NULL, 10);
  47. save = errno;
  48. if (p == NULL)
  49. FAIL_EXIT1 ("realloc (NULL, 10) failed.");
  50. free (p);
  51. p = calloc (20, 1);
  52. if (p == NULL)
  53. FAIL_EXIT1 ("calloc (20, 1) failed.");
  54. /* Check increasing size preserves contents (C89). */
  55. p = realloc (p, 200);
  56. if (p == NULL)
  57. FAIL_EXIT1 ("realloc (p, 200) failed.");
  58. c = p;
  59. ok = 1;
  60. for (i = 0; i < 20; i++)
  61. {
  62. if (c[i] != 0)
  63. ok = 0;
  64. }
  65. if (ok == 0)
  66. FAIL_EXIT1 ("first 20 bytes were not cleared");
  67. free (p);
  68. p = realloc (NULL, 100);
  69. if (p == NULL)
  70. FAIL_EXIT1 ("realloc (NULL, 100) failed.");
  71. memset (p, 0xff, 100);
  72. /* Check decreasing size preserves contents (C89). */
  73. p = realloc (p, 16);
  74. if (p == NULL)
  75. FAIL_EXIT1 ("realloc (p, 16) failed.");
  76. c = p;
  77. ok = 1;
  78. for (i = 0; i < 16; i++)
  79. {
  80. if (c[i] != 0xff)
  81. ok = 0;
  82. }
  83. if (ok == 0)
  84. FAIL_EXIT1 ("first 16 bytes were not correct");
  85. /* Check failed realloc leaves original untouched (C89). */
  86. DIAG_PUSH_NEEDS_COMMENT;
  87. #if __GNUC_PREREQ (7, 0)
  88. /* GCC 7 warns about too-large allocations; here we want to test
  89. that they fail. */
  90. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  91. #endif
  92. c = realloc (p, -1);
  93. DIAG_POP_NEEDS_COMMENT;
  94. if (c != NULL)
  95. FAIL_EXIT1 ("realloc (p, -1) succeeded.");
  96. c = p;
  97. ok = 1;
  98. for (i = 0; i < 16; i++)
  99. {
  100. if (c[i] != 0xff)
  101. ok = 0;
  102. }
  103. if (ok == 0)
  104. FAIL_EXIT1 ("first 16 bytes were not correct after failed realloc");
  105. /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
  106. p = realloc (p, 0);
  107. if (p != NULL)
  108. FAIL_EXIT1 ("realloc (p, 0) returned non-NULL.");
  109. /* realloc (NULL, 0) acts like malloc (0) (glibc). */
  110. p = realloc (NULL, 0);
  111. if (p == NULL)
  112. FAIL_EXIT1 ("realloc (NULL, 0) returned NULL.");
  113. free (p);
  114. /* Smoke test to make sure that allocations do not move if they have enough
  115. space to expand in the chunk. */
  116. for (size_t sz = 3; sz < 256 * 1024; sz += 2048)
  117. {
  118. p = realloc (NULL, sz);
  119. if (p == NULL)
  120. FAIL_EXIT1 ("realloc (NULL, %zu) returned NULL.", sz);
  121. size_t newsz = malloc_usable_size (p);
  122. printf ("size: %zu, usable size: %zu, extra: %zu\n",
  123. sz, newsz, newsz - sz);
  124. uintptr_t oldp = (uintptr_t) p;
  125. void *new_p = realloc (p, newsz);
  126. if ((uintptr_t) new_p != oldp)
  127. FAIL_EXIT1 ("Expanding (%zu bytes) to usable size (%zu) moved block",
  128. sz, newsz);
  129. free (new_p);
  130. /* We encountered a large enough extra size at least once. */
  131. if (newsz - sz > 1024)
  132. break;
  133. }
  134. return 0;
  135. }
  136. #define TEST_FUNCTION do_test ()
  137. #include "../test-skeleton.c"