1/* Copyright (C) 2000-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <error.h>
20#include <limits.h>
21#include <malloc.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <libc-diag.h>
25
26
27/* Number of samples per size. */
28#define N 50000
29
30
31static void
32fixed_test (int size)
33{
34 char *ptrs[N];
35 int i;
36
37 for (i = 0; i < N; ++i)
38 {
39 int j;
40
41 ptrs[i] = (char *) calloc (nmemb: 1, size: size);
42
43 if (ptrs[i] == NULL)
44 break;
45
46 for (j = 0; j < size; ++j)
47 {
48 if (ptrs[i][j] != '\0')
49 error (EXIT_FAILURE, errnum: 0,
50 format: "byte not cleared (size %d, element %d, byte %d)",
51 size, i, j);
52 ptrs[i][j] = '\xff';
53 }
54 }
55
56 while (i-- > 0)
57 free (ptr: ptrs[i]);
58}
59
60
61static void
62random_test (void)
63{
64 char *ptrs[N];
65 int i;
66
67 for (i = 0; i < N; ++i)
68 {
69 int j;
70 int n = 1 + random () % 10;
71 int elem = 1 + random () % 100;
72 int size = n * elem;
73
74 ptrs[i] = (char *) calloc (nmemb: n, size: elem);
75
76 if (ptrs[i] == NULL)
77 break;
78
79 for (j = 0; j < size; ++j)
80 {
81 if (ptrs[i][j] != '\0')
82 error (EXIT_FAILURE, errnum: 0,
83 format: "byte not cleared (size %d, element %d, byte %d)",
84 size, i, j);
85 ptrs[i][j] = '\xff';
86 }
87 }
88
89 while (i-- > 0)
90 free (ptr: ptrs[i]);
91}
92
93
94static void
95null_test (void)
96{
97 /* If the size is 0 the result is implementation defined. Just make
98 sure the program doesn't crash. The result of calloc is
99 deliberately ignored, so do not warn about that. */
100 DIAG_PUSH_NEEDS_COMMENT;
101 DIAG_IGNORE_NEEDS_COMMENT (10, "-Wunused-result");
102 calloc (nmemb: 0, size: 0);
103 calloc (nmemb: 0, UINT_MAX);
104 calloc (UINT_MAX, size: 0);
105 calloc (nmemb: 0, size: ~((size_t) 0));
106 calloc (nmemb: ~((size_t) 0), size: 0);
107 DIAG_POP_NEEDS_COMMENT;
108}
109
110
111static int
112do_test (void)
113{
114 /* We are allocating blocks with `calloc' and check whether every
115 block is completely cleared. We first try this for some fixed
116 times and then with random size. */
117 fixed_test (size: 15);
118 fixed_test (size: 5);
119 fixed_test (size: 17);
120 fixed_test (size: 6);
121 fixed_test (size: 31);
122 fixed_test (size: 96);
123
124 random_test ();
125
126 null_test ();
127
128 return 0;
129}
130
131#define TEST_FUNCTION do_test ()
132#include "../test-skeleton.c"
133

source code of glibc/malloc/tst-calloc.c