1/* Copyright (C) 2013-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 <stdint.h>
21#include <stdio.h>
22#include <string.h>
23#include <libc-diag.h>
24#include <support/check.h>
25
26#include "tst-malloc-aux.h"
27
28static int
29do_test (void)
30{
31 void *p;
32 unsigned char *c;
33 int save, i, ok;
34
35 errno = 0;
36
37 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
38 DIAG_PUSH_NEEDS_COMMENT;
39#if __GNUC_PREREQ (7, 0)
40 /* GCC 7 warns about too-large allocations; here we want to test
41 that they fail. */
42 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
43#endif
44 p = realloc (NULL, -1);
45 DIAG_POP_NEEDS_COMMENT;
46 save = errno;
47
48 if (p != NULL)
49 FAIL_EXIT1 ("realloc (NULL, -1) succeeded.");
50
51 /* errno should be set to ENOMEM on failure (POSIX). */
52 if (p == NULL && save != ENOMEM)
53 FAIL_EXIT1 ("errno is not set correctly");
54
55 errno = 0;
56
57 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
58 p = realloc (NULL, 10);
59 save = errno;
60
61 if (p == NULL)
62 FAIL_EXIT1 ("realloc (NULL, 10) failed.");
63
64 free (ptr: p);
65
66 p = calloc (20, 1);
67 if (p == NULL)
68 FAIL_EXIT1 ("calloc (20, 1) failed.");
69
70 /* Check increasing size preserves contents (C89). */
71 p = realloc (p, 200);
72 if (p == NULL)
73 FAIL_EXIT1 ("realloc (p, 200) failed.");
74
75 c = p;
76 ok = 1;
77
78 for (i = 0; i < 20; i++)
79 {
80 if (c[i] != 0)
81 ok = 0;
82 }
83
84 if (ok == 0)
85 FAIL_EXIT1 ("first 20 bytes were not cleared");
86
87 free (ptr: p);
88
89 p = realloc (NULL, 100);
90 if (p == NULL)
91 FAIL_EXIT1 ("realloc (NULL, 100) failed.");
92
93 memset (p, 0xff, 100);
94
95 /* Check decreasing size preserves contents (C89). */
96 p = realloc (p, 16);
97 if (p == NULL)
98 FAIL_EXIT1 ("realloc (p, 16) failed.");
99
100 c = p;
101 ok = 1;
102
103 for (i = 0; i < 16; i++)
104 {
105 if (c[i] != 0xff)
106 ok = 0;
107 }
108
109 if (ok == 0)
110 FAIL_EXIT1 ("first 16 bytes were not correct");
111
112 /* Check failed realloc leaves original untouched (C89). */
113 DIAG_PUSH_NEEDS_COMMENT;
114#if __GNUC_PREREQ (7, 0)
115 /* GCC 7 warns about too-large allocations; here we want to test
116 that they fail. */
117 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
118#endif
119 c = realloc (p, -1);
120 DIAG_POP_NEEDS_COMMENT;
121 if (c != NULL)
122 FAIL_EXIT1 ("realloc (p, -1) succeeded.");
123
124 c = p;
125 ok = 1;
126
127 for (i = 0; i < 16; i++)
128 {
129 if (c[i] != 0xff)
130 ok = 0;
131 }
132
133 if (ok == 0)
134 FAIL_EXIT1 ("first 16 bytes were not correct after failed realloc");
135
136 /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
137 p = realloc (p, 0);
138 if (p != NULL)
139 FAIL_EXIT1 ("realloc (p, 0) returned non-NULL.");
140
141 /* realloc (NULL, 0) acts like malloc (0) (glibc). */
142 p = realloc (NULL, 0);
143 if (p == NULL)
144 FAIL_EXIT1 ("realloc (NULL, 0) returned NULL.");
145
146 free (ptr: p);
147
148 /* Smoke test to make sure that allocations do not move if they have enough
149 space to expand in the chunk. */
150 for (size_t sz = 3; sz < 256 * 1024; sz += 2048)
151 {
152 p = realloc (NULL, sz);
153 if (p == NULL)
154 FAIL_EXIT1 ("realloc (NULL, %zu) returned NULL.", sz);
155 size_t newsz = malloc_usable_size (ptr: p);
156 printf (format: "size: %zu, usable size: %zu, extra: %zu\n",
157 sz, newsz, newsz - sz);
158 uintptr_t oldp = (uintptr_t) p;
159 void *new_p = realloc (p, newsz);
160 if ((uintptr_t) new_p != oldp)
161 FAIL_EXIT1 ("Expanding (%zu bytes) to usable size (%zu) moved block",
162 sz, newsz);
163 free (ptr: new_p);
164
165 /* We encountered a large enough extra size at least once. */
166 if (newsz - sz > 1024)
167 break;
168 }
169
170 return 0;
171}
172
173#define TEST_FUNCTION do_test ()
174#include "../test-skeleton.c"
175

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