]>
Commit | Line | Data |
---|---|---|
66a9be9d | 1 | /* Test for valloc. |
dff8da6b | 2 | Copyright (C) 2013-2024 Free Software Foundation, Inc. |
7815420b 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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
7815420b WN |
18 | |
19 | #include <errno.h> | |
68e8dcc7 | 20 | #include <stdlib.h> |
7815420b WN |
21 | #include <stdio.h> |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
9bf8e29c | 24 | #include <libc-diag.h> |
68e8dcc7 | 25 | |
7815420b WN |
26 | static int errors = 0; |
27 | ||
28 | static void | |
29 | merror (const char *msg) | |
30 | { | |
31 | ++errors; | |
32 | printf ("Error: %s\n", msg); | |
33 | } | |
34 | ||
35 | static int | |
36 | do_test (void) | |
68e8dcc7 | 37 | { |
7815420b | 38 | void *p; |
66a9be9d | 39 | unsigned long pagesize = getpagesize (); |
7815420b WN |
40 | unsigned long ptrval; |
41 | int save; | |
42 | ||
43 | errno = 0; | |
44 | ||
9bf8e29c AZ |
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 | |
66a9be9d WN |
51 | /* An attempt to allocate a huge value should return NULL and set |
52 | errno to ENOMEM. */ | |
7815420b | 53 | p = valloc (-1); |
9bf8e29c AZ |
54 | #if __GNUC_PREREQ (7, 0) |
55 | DIAG_POP_NEEDS_COMMENT; | |
56 | #endif | |
7815420b WN |
57 | |
58 | save = errno; | |
68e8dcc7 | 59 | |
7815420b WN |
60 | if (p != NULL) |
61 | merror ("valloc (-1) succeeded."); | |
68e8dcc7 | 62 | |
7815420b WN |
63 | if (p == NULL && save != ENOMEM) |
64 | merror ("valloc (-1) errno is not set correctly"); | |
68e8dcc7 | 65 | |
66a9be9d WN |
66 | free (p); |
67 | ||
7815420b WN |
68 | errno = 0; |
69 | ||
66a9be9d | 70 | /* Test to expose integer overflow in malloc internals from BZ #15856. */ |
7815420b WN |
71 | p = valloc (-pagesize); |
72 | ||
73 | save = errno; | |
74 | ||
75 | if (p != NULL) | |
76 | merror ("valloc (-pagesize) succeeded."); | |
77 | ||
78 | if (p == NULL && save != ENOMEM) | |
79 | merror ("valloc (-pagesize) errno is not set correctly"); | |
80 | ||
66a9be9d WN |
81 | free (p); |
82 | ||
83 | /* A zero-sized allocation should succeed with glibc, returning a | |
84 | non-NULL value. */ | |
7815420b WN |
85 | p = valloc (0); |
86 | ||
87 | if (p == NULL) | |
88 | merror ("valloc (0) failed."); | |
89 | ||
90 | free (p); | |
91 | ||
66a9be9d | 92 | /* Check the alignment of the returned pointer is correct. */ |
7815420b WN |
93 | p = valloc (32); |
94 | ||
95 | if (p == NULL) | |
96 | merror ("valloc (32) failed."); | |
97 | ||
66a9be9d | 98 | ptrval = (unsigned long) p; |
7815420b WN |
99 | |
100 | if ((ptrval & (pagesize - 1)) != 0) | |
101 | merror ("returned pointer is not page aligned."); | |
102 | ||
103 | free (p); | |
104 | ||
105 | return errors != 0; | |
68e8dcc7 | 106 | } |
7815420b WN |
107 | |
108 | #define TEST_FUNCTION do_test () | |
109 | #include "../test-skeleton.c" |