Fix build failure on tilepro due to unsupported atomics
[glibc.git] / malloc / tst-realloc.c
blob31a58bd0260c3522e4c0f536cf6c9dd58f7c6414
1 /* Copyright (C) 2013-2017 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/>. */
18 #include <errno.h>
19 #include <malloc.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <libc-diag.h>
24 static int errors = 0;
26 static void
27 merror (const char *msg)
29 ++errors;
30 printf ("Error: %s\n", msg);
33 static int
34 do_test (void)
36 void *p;
37 unsigned char *c;
38 int save, i, ok;
40 errno = 0;
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, -1);
50 DIAG_POP_NEEDS_COMMENT;
51 save = errno;
53 if (p != NULL)
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");
60 errno = 0;
62 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
63 p = realloc (NULL, 10);
64 save = errno;
66 if (p == NULL)
67 merror ("realloc (NULL, 10) failed.");
69 /* errno should be clear on success (POSIX). */
70 if (p != NULL && save != 0)
71 merror ("errno is set but should not be");
73 free (p);
75 p = calloc (20, 1);
76 if (p == NULL)
77 merror ("calloc (20, 1) failed.");
79 /* Check increasing size preserves contents (C89). */
80 p = realloc (p, 200);
81 if (p == NULL)
82 merror ("realloc (p, 200) failed.");
84 c = p;
85 ok = 1;
87 for (i = 0; i < 20; i++)
89 if (c[i] != 0)
90 ok = 0;
93 if (ok == 0)
94 merror ("first 20 bytes were not cleared");
96 free (p);
98 p = realloc (NULL, 100);
99 if (p == NULL)
100 merror ("realloc (NULL, 100) failed.");
102 memset (p, 0xff, 100);
104 /* Check decreasing size preserves contents (C89). */
105 p = realloc (p, 16);
106 if (p == NULL)
107 merror ("realloc (p, 16) failed.");
109 c = p;
110 ok = 1;
112 for (i = 0; i < 16; i++)
114 if (c[i] != 0xff)
115 ok = 0;
118 if (ok == 0)
119 merror ("first 16 bytes were not correct");
121 /* Check failed realloc leaves original untouched (C89). */
122 DIAG_PUSH_NEEDS_COMMENT;
123 #if __GNUC_PREREQ (7, 0)
124 /* GCC 7 warns about too-large allocations; here we want to test
125 that they fail. */
126 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
127 #endif
128 c = realloc (p, -1);
129 DIAG_POP_NEEDS_COMMENT;
130 if (c != NULL)
131 merror ("realloc (p, -1) succeeded.");
133 c = p;
134 ok = 1;
136 for (i = 0; i < 16; i++)
138 if (c[i] != 0xff)
139 ok = 0;
142 if (ok == 0)
143 merror ("first 16 bytes were not correct after failed realloc");
145 /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
146 p = realloc (p, 0);
147 if (p != NULL)
148 merror ("realloc (p, 0) returned non-NULL.");
150 /* realloc (NULL, 0) acts like malloc (0) (glibc). */
151 p = realloc (NULL, 0);
152 if (p == NULL)
153 merror ("realloc (NULL, 0) returned NULL.");
155 free (p);
157 return errors != 0;
160 #define TEST_FUNCTION do_test ()
161 #include "../test-skeleton.c"