1 | /* Copyright (C) 1999-2024 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <malloc.h> |
20 | #include <stdio.h> |
21 | #include <libc-diag.h> |
22 | |
23 | static int errors = 0; |
24 | |
25 | static void |
26 | merror (const char *msg) |
27 | { |
28 | ++errors; |
29 | printf (format: "Error: %s\n" , msg); |
30 | } |
31 | |
32 | static int |
33 | do_test (void) |
34 | { |
35 | void *p, *q; |
36 | int save; |
37 | |
38 | errno = 0; |
39 | |
40 | DIAG_PUSH_NEEDS_COMMENT; |
41 | #if __GNUC_PREREQ (7, 0) |
42 | /* GCC 7 warns about too-large allocations; here we want to test |
43 | that they fail. */ |
44 | DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=" ); |
45 | #endif |
46 | p = malloc (size: -1); |
47 | DIAG_POP_NEEDS_COMMENT; |
48 | save = errno; |
49 | |
50 | if (p != NULL) |
51 | merror (msg: "malloc (-1) succeeded." ); |
52 | |
53 | if (p == NULL && save != ENOMEM) |
54 | merror (msg: "errno is not set correctly" ); |
55 | |
56 | p = malloc (size: 10); |
57 | if (p == NULL) |
58 | merror (msg: "malloc (10) failed." ); |
59 | |
60 | /* realloc (p, 0) == free (p). */ |
61 | p = realloc (ptr: p, size: 0); |
62 | if (p != NULL) |
63 | merror (msg: "realloc (p, 0) failed." ); |
64 | |
65 | p = malloc (size: 0); |
66 | if (p == NULL) |
67 | merror (msg: "malloc (0) failed." ); |
68 | |
69 | p = realloc (ptr: p, size: 0); |
70 | if (p != NULL) |
71 | merror (msg: "realloc (p, 0) failed." ); |
72 | |
73 | p = malloc (size: 513 * 1024); |
74 | if (p == NULL) |
75 | merror (msg: "malloc (513K) failed." ); |
76 | |
77 | DIAG_PUSH_NEEDS_COMMENT; |
78 | #if __GNUC_PREREQ (7, 0) |
79 | /* GCC 7 warns about too-large allocations; here we want to test |
80 | that they fail. */ |
81 | DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=" ); |
82 | #endif |
83 | q = malloc (size: -512 * 1024); |
84 | DIAG_POP_NEEDS_COMMENT; |
85 | if (q != NULL) |
86 | merror (msg: "malloc (-512K) succeeded." ); |
87 | |
88 | free (ptr: p); |
89 | |
90 | return errors != 0; |
91 | } |
92 | |
93 | #define TEST_FUNCTION do_test () |
94 | #include "../test-skeleton.c" |
95 | |