tst-realloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright (C) 2013-2019 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. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <malloc.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <libc-diag.h>
  19. static int errors = 0;
  20. static void
  21. merror (const char *msg)
  22. {
  23. ++errors;
  24. printf ("Error: %s\n", msg);
  25. }
  26. static int
  27. do_test (void)
  28. {
  29. void *p;
  30. unsigned char *c;
  31. int save, i, ok;
  32. errno = 0;
  33. /* realloc (NULL, ...) behaves similarly to malloc (C89). */
  34. DIAG_PUSH_NEEDS_COMMENT;
  35. #if __GNUC_PREREQ (7, 0)
  36. /* GCC 7 warns about too-large allocations; here we want to test
  37. that they fail. */
  38. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  39. #endif
  40. p = realloc (NULL, -1);
  41. DIAG_POP_NEEDS_COMMENT;
  42. save = errno;
  43. if (p != NULL)
  44. merror ("realloc (NULL, -1) succeeded.");
  45. /* errno should be set to ENOMEM on failure (POSIX). */
  46. if (p == NULL && save != ENOMEM)
  47. merror ("errno is not set correctly");
  48. errno = 0;
  49. /* realloc (NULL, ...) behaves similarly to malloc (C89). */
  50. p = realloc (NULL, 10);
  51. save = errno;
  52. if (p == NULL)
  53. merror ("realloc (NULL, 10) failed.");
  54. free (p);
  55. p = calloc (20, 1);
  56. if (p == NULL)
  57. merror ("calloc (20, 1) failed.");
  58. /* Check increasing size preserves contents (C89). */
  59. p = realloc (p, 200);
  60. if (p == NULL)
  61. merror ("realloc (p, 200) failed.");
  62. c = p;
  63. ok = 1;
  64. for (i = 0; i < 20; i++)
  65. {
  66. if (c[i] != 0)
  67. ok = 0;
  68. }
  69. if (ok == 0)
  70. merror ("first 20 bytes were not cleared");
  71. free (p);
  72. p = realloc (NULL, 100);
  73. if (p == NULL)
  74. merror ("realloc (NULL, 100) failed.");
  75. memset (p, 0xff, 100);
  76. /* Check decreasing size preserves contents (C89). */
  77. p = realloc (p, 16);
  78. if (p == NULL)
  79. merror ("realloc (p, 16) failed.");
  80. c = p;
  81. ok = 1;
  82. for (i = 0; i < 16; i++)
  83. {
  84. if (c[i] != 0xff)
  85. ok = 0;
  86. }
  87. if (ok == 0)
  88. merror ("first 16 bytes were not correct");
  89. /* Check failed realloc leaves original untouched (C89). */
  90. DIAG_PUSH_NEEDS_COMMENT;
  91. #if __GNUC_PREREQ (7, 0)
  92. /* GCC 7 warns about too-large allocations; here we want to test
  93. that they fail. */
  94. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  95. #endif
  96. c = realloc (p, -1);
  97. DIAG_POP_NEEDS_COMMENT;
  98. if (c != NULL)
  99. merror ("realloc (p, -1) succeeded.");
  100. c = p;
  101. ok = 1;
  102. for (i = 0; i < 16; i++)
  103. {
  104. if (c[i] != 0xff)
  105. ok = 0;
  106. }
  107. if (ok == 0)
  108. merror ("first 16 bytes were not correct after failed realloc");
  109. /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
  110. p = realloc (p, 0);
  111. if (p != NULL)
  112. merror ("realloc (p, 0) returned non-NULL.");
  113. /* realloc (NULL, 0) acts like malloc (0) (glibc). */
  114. p = realloc (NULL, 0);
  115. if (p == NULL)
  116. merror ("realloc (NULL, 0) returned NULL.");
  117. free (p);
  118. return errors != 0;
  119. }
  120. #define TEST_FUNCTION do_test ()
  121. #include "../test-skeleton.c"