1 /* Copyright (C) 2013-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
22 #include <libc-diag.h>
24 static int errors
= 0;
27 merror (const char *msg
)
30 printf ("Error: %s\n", msg
);
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
47 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
49 p
= realloc (NULL
, -1);
50 DIAG_POP_NEEDS_COMMENT
;
54 merror ("realloc (NULL, -1) succeeded.");
56 /* errno should be set to ENOMEM on failure (POSIX). */
57 if (p
== NULL
&& save
!= ENOMEM
)
58 merror ("errno is not set correctly");
62 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
63 p
= realloc (NULL
, 10);
67 merror ("realloc (NULL, 10) failed.");
73 merror ("calloc (20, 1) failed.");
75 /* Check increasing size preserves contents (C89). */
78 merror ("realloc (p, 200) failed.");
83 for (i
= 0; i
< 20; i
++)
90 merror ("first 20 bytes were not cleared");
94 p
= realloc (NULL
, 100);
96 merror ("realloc (NULL, 100) failed.");
98 memset (p
, 0xff, 100);
100 /* Check decreasing size preserves contents (C89). */
103 merror ("realloc (p, 16) failed.");
108 for (i
= 0; i
< 16; i
++)
115 merror ("first 16 bytes were not correct");
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
122 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
125 DIAG_POP_NEEDS_COMMENT
;
127 merror ("realloc (p, -1) succeeded.");
132 for (i
= 0; i
< 16; i
++)
139 merror ("first 16 bytes were not correct after failed realloc");
141 /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
144 merror ("realloc (p, 0) returned non-NULL.");
146 /* realloc (NULL, 0) acts like malloc (0) (glibc). */
147 p
= realloc (NULL
, 0);
149 merror ("realloc (NULL, 0) returned NULL.");
156 #define TEST_FUNCTION do_test ()
157 #include "../test-skeleton.c"