1 /* Test for posix_memalign.
2 Copyright (C) 2013-2019 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 <http://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
);
40 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 ENOMEM and
52 p should remain NULL. */
53 ret
= posix_memalign (&p
, sizeof (void *), -1);
54 #if __GNUC_PREREQ (7, 0)
55 DIAG_POP_NEEDS_COMMENT
;
59 merror ("posix_memalign (&p, sizeof (void *), -1) succeeded.");
61 if (ret
== ENOMEM
&& p
!= NULL
)
62 merror ("returned an error but pointer was modified");
68 /* Test to expose integer overflow in malloc internals from BZ #15857. */
69 ret
= posix_memalign (&p
, pagesize
, -pagesize
);
72 merror ("posix_memalign (&p, pagesize, -pagesize) succeeded.");
78 /* Test to expose integer overflow in malloc internals from BZ #16038. */
79 ret
= posix_memalign (&p
, -1, pagesize
);
82 merror ("posix_memalign (&p, -1, pagesize) succeeded.");
88 /* A zero-sized allocation should succeed with glibc, returning zero
89 and setting p to a non-NULL value. */
90 ret
= posix_memalign (&p
, sizeof (void *), 0);
92 if (ret
!= 0 || p
== NULL
)
93 merror ("posix_memalign (&p, sizeof (void *), 0) failed.");
97 ret
= posix_memalign (&p
, 0x300, 10);
100 merror ("posix_memalign (&p, 0x300, 10) succeeded.");
102 ret
= posix_memalign (&p
, 0, 10);
105 merror ("posix_memalign (&p, 0, 10) succeeded.");
109 ret
= posix_memalign (&p
, 0x100, 10);
112 merror ("posix_memalign (&p, 0x100, 10) failed.");
114 if (ret
== 0 && p
== NULL
)
115 merror ("returned success but pointer is NULL");
117 ptrval
= (unsigned long) p
;
119 if (ret
== 0 && (ptrval
& 0xff) != 0)
120 merror ("pointer is not aligned to 0x100");
127 #define TEST_FUNCTION do_test ()
128 #include "../test-skeleton.c"