powerpc64: Fix by using the configure value $libc_cv_cc_submachine [BZ #31629]
[glibc.git] / malloc / tst-malloc.c
blobf7a6e4654c374d01cc76fc08b58480a4d972f232
1 /* Copyright (C) 1999-2024 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 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <malloc.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <libc-diag.h>
23 #include <time.h>
25 static int errors = 0;
27 static void
28 merror (const char *msg)
30 ++errors;
31 printf ("Error: %s\n", msg);
34 static int
35 do_test (void)
37 void *p, *q;
38 int save;
40 srandom (time (NULL));
42 errno = 0;
44 DIAG_PUSH_NEEDS_COMMENT;
45 #if __GNUC_PREREQ (7, 0)
46 /* GCC 7 warns about too-large allocations; here we want to test
47 that they fail. */
48 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
49 #endif
50 p = malloc (-1);
51 DIAG_POP_NEEDS_COMMENT;
52 save = errno;
54 if (p != NULL)
55 merror ("malloc (-1) succeeded.");
57 if (p == NULL && save != ENOMEM)
58 merror ("errno is not set correctly");
60 p = malloc (10);
61 if (p == NULL)
62 merror ("malloc (10) failed.");
64 /* realloc (p, 0) == free (p). */
65 p = realloc (p, 0);
66 if (p != NULL)
67 merror ("realloc (p, 0) failed.");
69 p = malloc (0);
70 if (p == NULL)
71 merror ("malloc (0) failed.");
73 p = realloc (p, 0);
74 if (p != NULL)
75 merror ("realloc (p, 0) failed.");
77 p = malloc (513 * 1024);
78 if (p == NULL)
79 merror ("malloc (513K) failed.");
81 DIAG_PUSH_NEEDS_COMMENT;
82 #if __GNUC_PREREQ (7, 0)
83 /* GCC 7 warns about too-large allocations; here we want to test
84 that they fail. */
85 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
86 #endif
87 q = malloc (-512 * 1024);
88 DIAG_POP_NEEDS_COMMENT;
89 if (q != NULL)
90 merror ("malloc (-512K) succeeded.");
92 free (p);
94 return errors != 0;
97 #define TEST_FUNCTION do_test ()
98 #include "../test-skeleton.c"