2 Copyright (C) 2013-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <libc-diag.h>
26 static int errors
= 0;
29 merror (const char *msg
)
32 printf ("Error: %s\n", msg
);
39 unsigned long pagesize
= getpagesize ();
45 DIAG_PUSH_NEEDS_COMMENT
;
46 #if __GNUC_PREREQ (7, 0)
47 /* GCC 7 warns about too-large allocations; here we want to test
49 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
51 /* An attempt to allocate a huge value should return NULL and set
53 p
= memalign (sizeof (void *), -1);
54 #if __GNUC_PREREQ (7, 0)
55 DIAG_POP_NEEDS_COMMENT
;
61 merror ("memalign (sizeof (void *), -1) succeeded.");
63 if (p
== NULL
&& save
!= ENOMEM
)
64 merror ("memalign (sizeof (void *), -1) errno is not set correctly");
70 /* Test to expose integer overflow in malloc internals from BZ #15857. */
71 p
= memalign (pagesize
, -pagesize
);
76 merror ("memalign (pagesize, -pagesize) succeeded.");
78 if (p
== NULL
&& save
!= ENOMEM
)
79 merror ("memalign (pagesize, -pagesize) errno is not set correctly");
85 /* Test to expose integer overflow in malloc internals from BZ #16038. */
86 p
= memalign (-1, pagesize
);
91 merror ("memalign (-1, pagesize) succeeded.");
93 if (p
== NULL
&& save
!= EINVAL
)
94 merror ("memalign (-1, pagesize) errno is not set correctly");
98 /* A zero-sized allocation should succeed with glibc, returning a
100 p
= memalign (sizeof (void *), 0);
103 merror ("memalign (sizeof (void *), 0) failed.");
107 /* Check the alignment of the returned pointer is correct. */
108 p
= memalign (0x100, 10);
111 merror ("memalign (0x100, 10) failed.");
113 ptrval
= (unsigned long) p
;
115 if ((ptrval
& 0xff) != 0)
116 merror ("pointer is not aligned to 0x100");
123 #define TEST_FUNCTION do_test ()
124 #include "../test-skeleton.c"