tst-calloc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2000-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 <error.h>
  16. #include <limits.h>
  17. #include <malloc.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <libc-diag.h>
  21. #include "tst-malloc-aux.h"
  22. /* Number of samples per size. */
  23. #define N 50000
  24. static void
  25. fixed_test (int size)
  26. {
  27. char *ptrs[N];
  28. int i;
  29. for (i = 0; i < N; ++i)
  30. {
  31. int j;
  32. ptrs[i] = (char *) calloc (1, size);
  33. if (ptrs[i] == NULL)
  34. break;
  35. for (j = 0; j < size; ++j)
  36. {
  37. if (ptrs[i][j] != '\0')
  38. error (EXIT_FAILURE, 0,
  39. "byte not cleared (size %d, element %d, byte %d)",
  40. size, i, j);
  41. ptrs[i][j] = '\xff';
  42. }
  43. }
  44. while (i-- > 0)
  45. free (ptrs[i]);
  46. }
  47. static void
  48. random_test (void)
  49. {
  50. char *ptrs[N];
  51. int i;
  52. for (i = 0; i < N; ++i)
  53. {
  54. int j;
  55. int n = 1 + random () % 10;
  56. int elem = 1 + random () % 100;
  57. int size = n * elem;
  58. ptrs[i] = (char *) calloc (n, elem);
  59. if (ptrs[i] == NULL)
  60. break;
  61. for (j = 0; j < size; ++j)
  62. {
  63. if (ptrs[i][j] != '\0')
  64. error (EXIT_FAILURE, 0,
  65. "byte not cleared (size %d, element %d, byte %d)",
  66. size, i, j);
  67. ptrs[i][j] = '\xff';
  68. }
  69. }
  70. while (i-- > 0)
  71. free (ptrs[i]);
  72. }
  73. static void
  74. null_test (void)
  75. {
  76. /* Obscure allocation size from the compiler. */
  77. volatile size_t max_size = UINT_MAX;
  78. volatile size_t zero_size = 0;
  79. /* If the size is 0 the result is implementation defined. Just make
  80. sure the program doesn't crash. The result of calloc is
  81. deliberately ignored, so do not warn about that. */
  82. DIAG_PUSH_NEEDS_COMMENT;
  83. DIAG_IGNORE_NEEDS_COMMENT (10, "-Wunused-result");
  84. calloc (0, 0);
  85. calloc (0, max_size);
  86. calloc (max_size, 0);
  87. calloc (0, ~zero_size);
  88. calloc (~zero_size, 0);
  89. DIAG_POP_NEEDS_COMMENT;
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. /* We are allocating blocks with `calloc' and check whether every
  95. block is completely cleared. We first try this for some fixed
  96. times and then with random size. */
  97. fixed_test (15);
  98. fixed_test (5);
  99. fixed_test (17);
  100. fixed_test (6);
  101. fixed_test (31);
  102. fixed_test (96);
  103. random_test ();
  104. null_test ();
  105. return 0;
  106. }
  107. #define TEST_FUNCTION do_test ()
  108. #include "../test-skeleton.c"