1/* Copyright (C) 2013-2022 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 <string.h>
22#include <libc-diag.h>
23
24static int errors = 0;
25
26static void
27merror (const char *msg)
28{
29 ++errors;
30 printf (format: "Error: %s\n", msg);
31}
32
33static int
34do_test (void)
35{
36 void *p;
37 unsigned char *c;
38 int save, i, ok;
39
40 errno = 0;
41
42 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
43 DIAG_PUSH_NEEDS_COMMENT;
44#if __GNUC_PREREQ (7, 0)
45 /* GCC 7 warns about too-large allocations; here we want to test
46 that they fail. */
47 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
48#endif
49 p = realloc (NULL, size: -1);
50 DIAG_POP_NEEDS_COMMENT;
51 save = errno;
52
53 if (p != NULL)
54 merror (msg: "realloc (NULL, -1) succeeded.");
55
56 /* errno should be set to ENOMEM on failure (POSIX). */
57 if (p == NULL && save != ENOMEM)
58 merror (msg: "errno is not set correctly");
59
60 errno = 0;
61
62 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
63 p = realloc (NULL, size: 10);
64 save = errno;
65
66 if (p == NULL)
67 merror (msg: "realloc (NULL, 10) failed.");
68
69 free (ptr: p);
70
71 p = calloc (nmemb: 20, size: 1);
72 if (p == NULL)
73 merror (msg: "calloc (20, 1) failed.");
74
75 /* Check increasing size preserves contents (C89). */
76 p = realloc (ptr: p, size: 200);
77 if (p == NULL)
78 merror (msg: "realloc (p, 200) failed.");
79
80 c = p;
81 ok = 1;
82
83 for (i = 0; i < 20; i++)
84 {
85 if (c[i] != 0)
86 ok = 0;
87 }
88
89 if (ok == 0)
90 merror (msg: "first 20 bytes were not cleared");
91
92 free (ptr: p);
93
94 p = realloc (NULL, size: 100);
95 if (p == NULL)
96 merror (msg: "realloc (NULL, 100) failed.");
97
98 memset (p, 0xff, 100);
99
100 /* Check decreasing size preserves contents (C89). */
101 p = realloc (ptr: p, size: 16);
102 if (p == NULL)
103 merror (msg: "realloc (p, 16) failed.");
104
105 c = p;
106 ok = 1;
107
108 for (i = 0; i < 16; i++)
109 {
110 if (c[i] != 0xff)
111 ok = 0;
112 }
113
114 if (ok == 0)
115 merror (msg: "first 16 bytes were not correct");
116
117 /* Check failed realloc leaves original untouched (C89). */
118 DIAG_PUSH_NEEDS_COMMENT;
119#if __GNUC_PREREQ (7, 0)
120 /* GCC 7 warns about too-large allocations; here we want to test
121 that they fail. */
122 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
123#endif
124 c = realloc (ptr: p, size: -1);
125 DIAG_POP_NEEDS_COMMENT;
126 if (c != NULL)
127 merror (msg: "realloc (p, -1) succeeded.");
128
129 c = p;
130 ok = 1;
131
132 for (i = 0; i < 16; i++)
133 {
134 if (c[i] != 0xff)
135 ok = 0;
136 }
137
138 if (ok == 0)
139 merror (msg: "first 16 bytes were not correct after failed realloc");
140
141#if __GNUC_PREREQ (12, 0)
142 /* Ignore a valid warning about using a pointer made indeterminate
143 by a prior call to realloc(). */
144 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
145#endif
146 /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
147 p = realloc (ptr: p, size: 0);
148#if __GNUC_PREREQ (12, 0)
149 DIAG_POP_NEEDS_COMMENT;
150#endif
151 if (p != NULL)
152 merror (msg: "realloc (p, 0) returned non-NULL.");
153
154 /* realloc (NULL, 0) acts like malloc (0) (glibc). */
155 p = realloc (NULL, size: 0);
156 if (p == NULL)
157 merror (msg: "realloc (NULL, 0) returned NULL.");
158
159 free (ptr: p);
160
161 return errors != 0;
162}
163
164#define TEST_FUNCTION do_test ()
165#include "../test-skeleton.c"
166

source code of glibc/malloc/tst-realloc.c