]> git.ipfire.org Git - thirdparty/glibc.git/blame - malloc/tst-malloc-check.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / malloc / tst-malloc-check.c
CommitLineData
6d7e8eda 1/* Copyright (C) 2005-2023 Free Software Foundation, Inc.
bfc832cc 2 This file is part of the GNU C Library.
bfc832cc
UD
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
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
bfc832cc
UD
17
18#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
e15f7de6 21#include <libc-diag.h>
bfc832cc
UD
22
23static int errors = 0;
24
25static void
26merror (const char *msg)
27{
28 ++errors;
29 printf ("Error: %s\n", msg);
30}
31
29955b5d
AS
32static int
33do_test (void)
bfc832cc
UD
34{
35 void *p, *q;
36
37 errno = 0;
38
3d7229c2
JM
39 DIAG_PUSH_NEEDS_COMMENT;
40#if __GNUC_PREREQ (7, 0)
41 /* GCC 7 warns about too-large allocations; here we want to test
42 that they fail. */
43 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
44#endif
bfc832cc 45 p = malloc (-1);
3d7229c2 46 DIAG_POP_NEEDS_COMMENT;
bfc832cc
UD
47
48 if (p != NULL)
49 merror ("malloc (-1) succeeded.");
50 else if (errno != ENOMEM)
51 merror ("errno is not set correctly.");
52
53 p = malloc (10);
54 if (p == NULL)
55 merror ("malloc (10) failed.");
56
57 p = realloc (p, 0);
58 if (p != NULL)
59 merror ("realloc (p, 0) failed.");
60
61 p = malloc (0);
62 if (p == NULL)
63 merror ("malloc (0) failed.");
64
65 p = realloc (p, 0);
66 if (p != NULL)
67 merror ("realloc (p, 0) failed.");
68
69 q = malloc (256);
70 if (q == NULL)
71 merror ("malloc (256) failed.");
72
73 p = malloc (512);
74 if (p == NULL)
75 merror ("malloc (512) failed.");
76
3d7229c2
JM
77 DIAG_PUSH_NEEDS_COMMENT;
78#if __GNUC_PREREQ (7, 0)
79 /* GCC 7 warns about too-large allocations; here we want to test
80 that they fail. */
81 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
82#endif
bfc832cc
UD
83 if (realloc (p, -256) != NULL)
84 merror ("realloc (p, -256) succeeded.");
85 else if (errno != ENOMEM)
86 merror ("errno is not set correctly.");
3d7229c2 87 DIAG_POP_NEEDS_COMMENT;
bfc832cc 88
c094c232
MS
89#if __GNUC_PREREQ (12, 0)
90 /* Ignore a valid warning about using a pointer made indeterminate
91 by a prior call to realloc(). */
92 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
93#endif
bfc832cc 94 free (p);
c094c232
MS
95#if __GNUC_PREREQ (12, 0)
96 DIAG_POP_NEEDS_COMMENT;
97#endif
bfc832cc
UD
98
99 p = malloc (512);
100 if (p == NULL)
101 merror ("malloc (512) failed.");
102
3d7229c2
JM
103 DIAG_PUSH_NEEDS_COMMENT;
104#if __GNUC_PREREQ (7, 0)
105 /* GCC 7 warns about too-large allocations; here we want to test
106 that they fail. */
107 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
108#endif
bfc832cc
UD
109 if (realloc (p, -1) != NULL)
110 merror ("realloc (p, -1) succeeded.");
111 else if (errno != ENOMEM)
112 merror ("errno is not set correctly.");
3d7229c2 113 DIAG_POP_NEEDS_COMMENT;
bfc832cc 114
c094c232
MS
115#if __GNUC_PREREQ (12, 0)
116 /* Ignore a valid warning about using a pointer made indeterminate
117 by a prior call to realloc(). */
118 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
119#endif
bfc832cc 120 free (p);
c094c232
MS
121#if __GNUC_PREREQ (12, 0)
122 DIAG_POP_NEEDS_COMMENT;
123#endif
bfc832cc
UD
124 free (q);
125
126 return errors != 0;
127}
29955b5d
AS
128
129#define TEST_FUNCTION do_test ()
130#include "../test-skeleton.c"