1 | /* Copyright (C) 2005-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 <stdio.h> |
20 | #include <stdlib.h> |
21 | #include <libc-diag.h> |
22 | |
23 | #include "tst-malloc-aux.h" |
24 | |
25 | static int errors = 0; |
26 | |
27 | static void |
28 | merror (const char *msg) |
29 | { |
30 | ++errors; |
31 | printf (format: "Error: %s\n" , msg); |
32 | } |
33 | |
34 | static int |
35 | do_test (void) |
36 | { |
37 | void *p, *q; |
38 | |
39 | errno = 0; |
40 | |
41 | DIAG_PUSH_NEEDS_COMMENT; |
42 | #if __GNUC_PREREQ (7, 0) |
43 | /* GCC 7 warns about too-large allocations; here we want to test |
44 | that they fail. */ |
45 | DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=" ); |
46 | #endif |
47 | p = malloc (-1); |
48 | DIAG_POP_NEEDS_COMMENT; |
49 | |
50 | if (p != NULL) |
51 | merror (msg: "malloc (-1) succeeded." ); |
52 | else if (errno != ENOMEM) |
53 | merror (msg: "errno is not set correctly." ); |
54 | |
55 | p = malloc (10); |
56 | if (p == NULL) |
57 | merror (msg: "malloc (10) failed." ); |
58 | |
59 | p = realloc (p, 0); |
60 | if (p != NULL) |
61 | merror (msg: "realloc (p, 0) failed." ); |
62 | |
63 | p = malloc (0); |
64 | if (p == NULL) |
65 | merror (msg: "malloc (0) failed." ); |
66 | |
67 | p = realloc (p, 0); |
68 | if (p != NULL) |
69 | merror (msg: "realloc (p, 0) failed." ); |
70 | |
71 | q = malloc (256); |
72 | if (q == NULL) |
73 | merror (msg: "malloc (256) failed." ); |
74 | |
75 | p = malloc (512); |
76 | if (p == NULL) |
77 | merror (msg: "malloc (512) failed." ); |
78 | |
79 | DIAG_PUSH_NEEDS_COMMENT; |
80 | #if __GNUC_PREREQ (7, 0) |
81 | /* GCC 7 warns about too-large allocations; here we want to test |
82 | that they fail. */ |
83 | DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=" ); |
84 | #endif |
85 | if (realloc (p, -256) != NULL) |
86 | merror (msg: "realloc (p, -256) succeeded." ); |
87 | else if (errno != ENOMEM) |
88 | merror (msg: "errno is not set correctly." ); |
89 | DIAG_POP_NEEDS_COMMENT; |
90 | |
91 | #if __GNUC_PREREQ (12, 0) |
92 | /* Ignore a valid warning about using a pointer made indeterminate |
93 | by a prior call to realloc(). */ |
94 | DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free" ); |
95 | #endif |
96 | free (ptr: p); |
97 | #if __GNUC_PREREQ (12, 0) |
98 | DIAG_POP_NEEDS_COMMENT; |
99 | #endif |
100 | |
101 | p = malloc (512); |
102 | if (p == NULL) |
103 | merror (msg: "malloc (512) failed." ); |
104 | |
105 | DIAG_PUSH_NEEDS_COMMENT; |
106 | #if __GNUC_PREREQ (7, 0) |
107 | /* GCC 7 warns about too-large allocations; here we want to test |
108 | that they fail. */ |
109 | DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=" ); |
110 | #endif |
111 | if (realloc (p, -1) != NULL) |
112 | merror (msg: "realloc (p, -1) succeeded." ); |
113 | else if (errno != ENOMEM) |
114 | merror (msg: "errno is not set correctly." ); |
115 | DIAG_POP_NEEDS_COMMENT; |
116 | |
117 | #if __GNUC_PREREQ (12, 0) |
118 | /* Ignore a valid warning about using a pointer made indeterminate |
119 | by a prior call to realloc(). */ |
120 | DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free" ); |
121 | #endif |
122 | free (ptr: p); |
123 | #if __GNUC_PREREQ (12, 0) |
124 | DIAG_POP_NEEDS_COMMENT; |
125 | #endif |
126 | free (ptr: q); |
127 | |
128 | return errors != 0; |
129 | } |
130 | |
131 | #define TEST_FUNCTION do_test () |
132 | #include "../test-skeleton.c" |
133 | |