memalignment: document
[gnulib.git] / tests / test-malloc-posix.c
blob1d00abe2e4ee768b0becadf8e501ecfeeec20488
1 /* Test of malloc function.
2 Copyright (C) 2024-2025 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/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <stdlib.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdint.h>
26 #include "macros.h"
28 /* Work around clang bug
29 <https://github.com/llvm/llvm-project/issues/114772>. */
30 void *(*volatile my_malloc) (size_t) = malloc;
31 #undef malloc
32 #define malloc my_malloc
34 int
35 main ()
37 /* Check that malloc 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)
42 void *p;
44 errno = 0;
45 p = malloc (SIZE_MAX / 10);
46 ASSERT (p == NULL);
47 ASSERT (errno == ENOMEM);
49 errno = 0;
50 p = malloc (SIZE_MAX / 3);
51 ASSERT (p == NULL);
52 ASSERT (errno == ENOMEM);
54 return test_exit_status;
56 else
58 fputs ("Skipping test: size_t is not 64-bits wide\n", stderr);
59 return 77;