]>
Commit | Line | Data |
---|---|---|
66a9be9d | 1 | /* Test for valloc. |
f7a9f785 | 2 | Copyright (C) 2013-2016 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 | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <errno.h> | |
68e8dcc7 | 20 | #include <stdlib.h> |
7815420b WN |
21 | #include <stdio.h> |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
68e8dcc7 | 24 | |
7815420b WN |
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) | |
68e8dcc7 | 36 | { |
7815420b | 37 | void *p; |
66a9be9d | 38 | unsigned long pagesize = getpagesize (); |
7815420b WN |
39 | unsigned long ptrval; |
40 | int save; | |
41 | ||
42 | errno = 0; | |
43 | ||
66a9be9d WN |
44 | /* An attempt to allocate a huge value should return NULL and set |
45 | errno to ENOMEM. */ | |
7815420b WN |
46 | p = valloc (-1); |
47 | ||
48 | save = errno; | |
68e8dcc7 | 49 | |
7815420b WN |
50 | if (p != NULL) |
51 | merror ("valloc (-1) succeeded."); | |
68e8dcc7 | 52 | |
7815420b WN |
53 | if (p == NULL && save != ENOMEM) |
54 | merror ("valloc (-1) errno is not set correctly"); | |
68e8dcc7 | 55 | |
66a9be9d WN |
56 | free (p); |
57 | ||
7815420b WN |
58 | errno = 0; |
59 | ||
66a9be9d | 60 | /* Test to expose integer overflow in malloc internals from BZ #15856. */ |
7815420b WN |
61 | p = valloc (-pagesize); |
62 | ||
63 | save = errno; | |
64 | ||
65 | if (p != NULL) | |
66 | merror ("valloc (-pagesize) succeeded."); | |
67 | ||
68 | if (p == NULL && save != ENOMEM) | |
69 | merror ("valloc (-pagesize) errno is not set correctly"); | |
70 | ||
66a9be9d WN |
71 | free (p); |
72 | ||
73 | /* A zero-sized allocation should succeed with glibc, returning a | |
74 | non-NULL value. */ | |
7815420b WN |
75 | p = valloc (0); |
76 | ||
77 | if (p == NULL) | |
78 | merror ("valloc (0) failed."); | |
79 | ||
80 | free (p); | |
81 | ||
66a9be9d | 82 | /* Check the alignment of the returned pointer is correct. */ |
7815420b WN |
83 | p = valloc (32); |
84 | ||
85 | if (p == NULL) | |
86 | merror ("valloc (32) failed."); | |
87 | ||
66a9be9d | 88 | ptrval = (unsigned long) p; |
7815420b WN |
89 | |
90 | if ((ptrval & (pagesize - 1)) != 0) | |
91 | merror ("returned pointer is not page aligned."); | |
92 | ||
93 | free (p); | |
94 | ||
95 | return errors != 0; | |
68e8dcc7 | 96 | } |
7815420b WN |
97 | |
98 | #define TEST_FUNCTION do_test () | |
99 | #include "../test-skeleton.c" |