Debian Codegraph @

gnulib-tests/test-calloc-posix.c

raw
1 /* Test of calloc function.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include <stdlib.h>
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdint.h>
25
26 #include "macros.h"
27
28 /* Work around clang bug
29 <https://github.com/llvm/llvm-project/issues/114772>. */
30 void *(*volatile my_calloc) (size_t, size_t) = calloc;
31 #undef calloc
32 #define calloc my_calloc
33
34 int
35 main ()
36 {
37 /* Check that calloc sets errno when it fails.
38 Do this only in 64-bit processes, because there are many bi-arch systems
39 nowadays where a 32-bit process can actually allocate 2 GiB of RAM. */
40 if (sizeof (size_t) >= 8)
41 {
42 void *p;
43
44 errno = 0;
45 p = calloc (SIZE_MAX / 20, 2);
46 ASSERT (p == NULL);
47 ASSERT (errno == ENOMEM);
48
49 errno = 0;
50 p = calloc (SIZE_MAX / 6, 2);
51 ASSERT (p == NULL);
52 ASSERT (errno == ENOMEM);
53
54 return test_exit_status;
55 }
56 else
57 {
58 fputs ("Skipping test: size_t is not 64-bits wide\n", stderr);
59 return 77;
60 }
61 }