tst-reallocarray.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Test for reallocarray.
  2. Copyright (C) 2017-2019 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. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include <support/check.h>
  19. static int
  20. do_test (void)
  21. {
  22. void *ptr = NULL;
  23. void *ptr2 = NULL;
  24. unsigned char *c;
  25. size_t i;
  26. int ok;
  27. const size_t max = ~(size_t)0;
  28. size_t a, b;
  29. /* Test overflow detection. */
  30. errno = 0;
  31. ptr = reallocarray (NULL, max, 2);
  32. TEST_VERIFY (!ptr);
  33. TEST_VERIFY (errno == ENOMEM);
  34. errno = 0;
  35. ptr = reallocarray (NULL, 2, max);
  36. TEST_VERIFY (!ptr);
  37. TEST_VERIFY (errno == ENOMEM);
  38. a = 65537;
  39. b = max/65537 + 1;
  40. errno = 0;
  41. ptr = reallocarray (NULL, a, b);
  42. TEST_VERIFY (!ptr);
  43. TEST_VERIFY (errno == ENOMEM);
  44. errno = 0;
  45. ptr = reallocarray (NULL, b, a);
  46. TEST_VERIFY (!ptr);
  47. TEST_VERIFY (errno == ENOMEM);
  48. /* Test realloc-like behavior. */
  49. /* Allocate memory like malloc. */
  50. ptr = reallocarray (NULL, 10, 2);
  51. TEST_VERIFY_EXIT (ptr);
  52. TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 10*2);
  53. memset (ptr, 0xAF, 10*2);
  54. /* Enlarge buffer. */
  55. ptr2 = reallocarray (ptr, 20, 2);
  56. TEST_VERIFY (ptr2);
  57. if (ptr2)
  58. ptr = ptr2;
  59. TEST_VERIFY (malloc_usable_size (ptr) >= 20*2);
  60. c = ptr;
  61. ok = 1;
  62. for (i = 0; i < 10*2; ++i)
  63. {
  64. if (c[i] != 0xAF)
  65. ok = 0;
  66. }
  67. TEST_VERIFY (ok);
  68. /* Decrease buffer size. */
  69. ptr2 = reallocarray (ptr, 5, 3);
  70. TEST_VERIFY (ptr2);
  71. if (ptr2)
  72. ptr = ptr2;
  73. TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 5*3);
  74. c = ptr;
  75. ok = 1;
  76. for (i = 0; i < 5*3; ++i)
  77. {
  78. if (c[i] != 0xAF)
  79. ok = 0;
  80. }
  81. TEST_VERIFY (ok);
  82. /* Overflow should leave buffer untouched. */
  83. errno = 0;
  84. ptr2 = reallocarray (ptr, 2, ~(size_t)0);
  85. TEST_VERIFY (!ptr2);
  86. TEST_VERIFY (errno == ENOMEM);
  87. c = ptr;
  88. ok = 1;
  89. for (i = 0; i < 5*3; ++i)
  90. {
  91. if (c[i] != 0xAF)
  92. ok = 0;
  93. }
  94. TEST_VERIFY (ok);
  95. free (ptr);
  96. return 0;
  97. }
  98. #include <support/test-driver.c>