| 1 | /* Test for valloc. |
| 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 <stdlib.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 | |
| 28 | static int errors = 0; |
| 29 | |
| 30 | static void |
| 31 | merror (const char *msg) |
| 32 | { |
| 33 | ++errors; |
| 34 | printf (format: "Error: %s\n" , msg); |
| 35 | } |
| 36 | |
| 37 | static int |
| 38 | do_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 = valloc (-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: "valloc (-1) succeeded." ); |
| 64 | |
| 65 | if (p == NULL && save != ENOMEM) |
| 66 | merror (msg: "valloc (-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 #15856. */ |
| 73 | p = valloc (-pagesize); |
| 74 | |
| 75 | save = errno; |
| 76 | |
| 77 | if (p != NULL) |
| 78 | merror (msg: "valloc (-pagesize) succeeded." ); |
| 79 | |
| 80 | if (p == NULL && save != ENOMEM) |
| 81 | merror (msg: "valloc (-pagesize) errno is not set correctly" ); |
| 82 | |
| 83 | free (ptr: p); |
| 84 | |
| 85 | /* A zero-sized allocation should succeed with glibc, returning a |
| 86 | non-NULL value. */ |
| 87 | p = valloc (0); |
| 88 | |
| 89 | if (p == NULL) |
| 90 | merror (msg: "valloc (0) failed." ); |
| 91 | |
| 92 | free (ptr: p); |
| 93 | |
| 94 | /* Check the alignment of the returned pointer is correct. */ |
| 95 | p = valloc (32); |
| 96 | |
| 97 | if (p == NULL) |
| 98 | merror (msg: "valloc (32) failed." ); |
| 99 | |
| 100 | ptrval = (unsigned long) p; |
| 101 | |
| 102 | if ((ptrval & (pagesize - 1)) != 0) |
| 103 | merror (msg: "returned pointer is not page aligned." ); |
| 104 | |
| 105 | free (ptr: p); |
| 106 | |
| 107 | return errors != 0; |
| 108 | } |
| 109 | |
| 110 | #define TEST_FUNCTION do_test () |
| 111 | #include "../test-skeleton.c" |
| 112 | |