1 /* Test of calloc function.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
28 Usual compilers are not able to infer something about the return value. */
32 unsigned int x
= rand ();
33 unsigned int y
= x
* x
* x
* x
;
34 x
++; y
|= x
* x
* x
* x
;
35 x
++; y
|= x
* x
* x
* x
;
36 x
++; y
|= x
* x
* x
* x
;
40 /* At this point Y is zero but GCC doesn't infer this. */
47 /* Check that calloc (0, 0) is not a NULL pointer. */
49 void * volatile p
= calloc (0, 0);
54 /* Check that calloc fails when requested to allocate a block of memory
55 larger than PTRDIFF_MAX or SIZE_MAX bytes.
56 Use 'identity' to avoid a compiler warning from GCC 7.
57 'volatile' is needed to defeat an incorrect optimization by clang 10,
58 see <https://bugs.llvm.org/show_bug.cgi?id=46055>. */
60 for (size_t n
= 2; n
!= 0; n
<<= 1)
62 void *volatile p
= calloc (PTRDIFF_MAX
/ n
+ 1, identity (n
));
64 ASSERT (errno
== ENOMEM
);
66 p
= calloc (SIZE_MAX
/ n
+ 1, identity (n
));
68 ASSERT (errno
== ENOMEM
);
72 return test_exit_status
;