]>
Commit | Line | Data |
---|---|---|
27d0461b | 1 | /* Test for posix_memalign. |
bfff8b1b | 2 | Copyright (C) 2013-2017 Free Software Foundation, Inc. |
4868b204 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 <stdlib.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 | int ret; | |
27d0461b | 39 | unsigned long pagesize = getpagesize (); |
4868b204 WN |
40 | unsigned long ptrval; |
41 | ||
42 | p = NULL; | |
43 | ||
27d0461b WN |
44 | /* An attempt to allocate a huge value should return ENOMEM and |
45 | p should remain NULL. */ | |
4868b204 WN |
46 | ret = posix_memalign (&p, sizeof (void *), -1); |
47 | ||
48 | if (ret != ENOMEM) | |
49 | merror ("posix_memalign (&p, sizeof (void *), -1) succeeded."); | |
50 | ||
51 | if (ret == ENOMEM && p != NULL) | |
52 | merror ("returned an error but pointer was modified"); | |
53 | ||
27d0461b WN |
54 | free (p); |
55 | ||
4868b204 WN |
56 | p = NULL; |
57 | ||
27d0461b | 58 | /* Test to expose integer overflow in malloc internals from BZ #15857. */ |
4868b204 WN |
59 | ret = posix_memalign (&p, pagesize, -pagesize); |
60 | ||
61 | if (ret != ENOMEM) | |
62 | merror ("posix_memalign (&p, pagesize, -pagesize) succeeded."); | |
63 | ||
27d0461b WN |
64 | free (p); |
65 | ||
4868b204 WN |
66 | p = NULL; |
67 | ||
a56ee40b WN |
68 | /* Test to expose integer overflow in malloc internals from BZ #16038. */ |
69 | ret = posix_memalign (&p, -1, pagesize); | |
70 | ||
71 | if (ret != EINVAL) | |
72 | merror ("posix_memalign (&p, -1, pagesize) succeeded."); | |
73 | ||
74 | free (p); | |
75 | ||
76 | p = NULL; | |
77 | ||
27d0461b WN |
78 | /* A zero-sized allocation should succeed with glibc, returning zero |
79 | and setting p to a non-NULL value. */ | |
4868b204 WN |
80 | ret = posix_memalign (&p, sizeof (void *), 0); |
81 | ||
82 | if (ret != 0 || p == NULL) | |
83 | merror ("posix_memalign (&p, sizeof (void *), 0) failed."); | |
84 | ||
85 | free (p); | |
86 | ||
87 | ret = posix_memalign (&p, 0x300, 10); | |
88 | ||
89 | if (ret != EINVAL) | |
90 | merror ("posix_memalign (&p, 0x300, 10) succeeded."); | |
91 | ||
92 | ret = posix_memalign (&p, 0, 10); | |
93 | ||
94 | if (ret != EINVAL) | |
95 | merror ("posix_memalign (&p, 0, 10) succeeded."); | |
96 | ||
97 | p = NULL; | |
98 | ||
99 | ret = posix_memalign (&p, 0x100, 10); | |
100 | ||
101 | if (ret != 0) | |
102 | merror ("posix_memalign (&p, 0x100, 10) failed."); | |
103 | ||
104 | if (ret == 0 && p == NULL) | |
105 | merror ("returned success but pointer is NULL"); | |
106 | ||
27d0461b | 107 | ptrval = (unsigned long) p; |
4868b204 | 108 | |
27d0461b | 109 | if (ret == 0 && (ptrval & 0xff) != 0) |
4868b204 WN |
110 | merror ("pointer is not aligned to 0x100"); |
111 | ||
112 | free (p); | |
113 | ||
114 | return errors != 0; | |
115 | } | |
116 | ||
117 | #define TEST_FUNCTION do_test () | |
118 | #include "../test-skeleton.c" |