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

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