1/* Test for memalign.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <errno.h>
20#include <malloc.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24#include <libc-diag.h>
25
26static int errors = 0;
27
28static void
29merror (const char *msg)
30{
31 ++errors;
32 printf (format: "Error: %s\n", msg);
33}
34
35static int
36do_test (void)
37{
38 void *p;
39 unsigned long pagesize = getpagesize ();
40 unsigned long ptrval;
41 int save;
42
43 errno = 0;
44
45 DIAG_PUSH_NEEDS_COMMENT;
46#if __GNUC_PREREQ (7, 0)
47 /* GCC 7 warns about too-large allocations; here we want to test
48 that they fail. */
49 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
50#endif
51 /* An attempt to allocate a huge value should return NULL and set
52 errno to ENOMEM. */
53 p = memalign (alignment: sizeof (void *), size: -1);
54#if __GNUC_PREREQ (7, 0)
55 DIAG_POP_NEEDS_COMMENT;
56#endif
57
58 save = errno;
59
60 if (p != NULL)
61 merror (msg: "memalign (sizeof (void *), -1) succeeded.");
62
63 if (p == NULL && save != ENOMEM)
64 merror (msg: "memalign (sizeof (void *), -1) errno is not set correctly");
65
66 free (ptr: p);
67
68 errno = 0;
69
70 /* Test to expose integer overflow in malloc internals from BZ #15857. */
71 p = memalign (alignment: pagesize, size: -pagesize);
72
73 save = errno;
74
75 if (p != NULL)
76 merror (msg: "memalign (pagesize, -pagesize) succeeded.");
77
78 if (p == NULL && save != ENOMEM)
79 merror (msg: "memalign (pagesize, -pagesize) errno is not set correctly");
80
81 free (ptr: p);
82
83 errno = 0;
84
85 /* Test to expose integer overflow in malloc internals from BZ #16038. */
86 p = memalign (alignment: -1, size: pagesize);
87
88 save = errno;
89
90 if (p != NULL)
91 merror (msg: "memalign (-1, pagesize) succeeded.");
92
93 if (p == NULL && save != EINVAL)
94 merror (msg: "memalign (-1, pagesize) errno is not set correctly");
95
96 free (ptr: p);
97
98 /* A zero-sized allocation should succeed with glibc, returning a
99 non-NULL value. */
100 p = memalign (alignment: sizeof (void *), size: 0);
101
102 if (p == NULL)
103 merror (msg: "memalign (sizeof (void *), 0) failed.");
104
105 free (ptr: p);
106
107 /* Check the alignment of the returned pointer is correct. */
108 p = memalign (alignment: 0x100, size: 10);
109
110 if (p == NULL)
111 merror (msg: "memalign (0x100, 10) failed.");
112
113 ptrval = (unsigned long) p;
114
115 if ((ptrval & 0xff) != 0)
116 merror (msg: "pointer is not aligned to 0x100");
117
118 free (ptr: p);
119
120 return errors != 0;
121}
122
123#define TEST_FUNCTION do_test ()
124#include "../test-skeleton.c"
125

source code of glibc/malloc/tst-memalign.c