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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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