]>
Commit | Line | Data |
---|---|---|
215c7d43 | 1 | /* Test for memalign. |
688903eb | 2 | Copyright (C) 2013-2018 Free Software Foundation, Inc. |
215c7d43 WN |
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 | <http://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 | ||
25 | static int errors = 0; | |
26 | ||
27 | static void | |
28 | merror (const char *msg) | |
29 | { | |
30 | ++errors; | |
31 | printf ("Error: %s\n", msg); | |
32 | } | |
33 | ||
34 | static int | |
35 | do_test (void) | |
36 | { | |
37 | void *p; | |
38 | unsigned long pagesize = getpagesize (); | |
39 | unsigned long ptrval; | |
40 | int save; | |
41 | ||
42 | errno = 0; | |
43 | ||
44 | /* An attempt to allocate a huge value should return NULL and set | |
45 | errno to ENOMEM. */ | |
46 | p = memalign (sizeof (void *), -1); | |
47 | ||
48 | save = errno; | |
49 | ||
50 | if (p != NULL) | |
51 | merror ("memalign (sizeof (void *), -1) succeeded."); | |
52 | ||
53 | if (p == NULL && save != ENOMEM) | |
54 | merror ("memalign (sizeof (void *), -1) errno is not set correctly"); | |
55 | ||
56 | free (p); | |
57 | ||
58 | errno = 0; | |
59 | ||
60 | /* Test to expose integer overflow in malloc internals from BZ #15857. */ | |
61 | p = memalign (pagesize, -pagesize); | |
62 | ||
63 | save = errno; | |
64 | ||
65 | if (p != NULL) | |
66 | merror ("memalign (pagesize, -pagesize) succeeded."); | |
67 | ||
68 | if (p == NULL && save != ENOMEM) | |
69 | merror ("memalign (pagesize, -pagesize) errno is not set correctly"); | |
70 | ||
71 | free (p); | |
72 | ||
a56ee40b WN |
73 | errno = 0; |
74 | ||
75 | /* Test to expose integer overflow in malloc internals from BZ #16038. */ | |
76 | p = memalign (-1, pagesize); | |
77 | ||
78 | save = errno; | |
79 | ||
80 | if (p != NULL) | |
81 | merror ("memalign (-1, pagesize) succeeded."); | |
82 | ||
83 | if (p == NULL && save != EINVAL) | |
84 | merror ("memalign (-1, pagesize) errno is not set correctly"); | |
85 | ||
86 | free (p); | |
87 | ||
215c7d43 WN |
88 | /* A zero-sized allocation should succeed with glibc, returning a |
89 | non-NULL value. */ | |
90 | p = memalign (sizeof (void *), 0); | |
91 | ||
92 | if (p == NULL) | |
93 | merror ("memalign (sizeof (void *), 0) failed."); | |
94 | ||
95 | free (p); | |
96 | ||
97 | /* Check the alignment of the returned pointer is correct. */ | |
98 | p = memalign (0x100, 10); | |
99 | ||
100 | if (p == NULL) | |
101 | merror ("memalign (0x100, 10) failed."); | |
102 | ||
103 | ptrval = (unsigned long) p; | |
104 | ||
105 | if ((ptrval & 0xff) != 0) | |
106 | merror ("pointer is not aligned to 0x100"); | |
107 | ||
108 | free (p); | |
109 | ||
110 | return errors != 0; | |
111 | } | |
112 | ||
113 | #define TEST_FUNCTION do_test () | |
114 | #include "../test-skeleton.c" |