]> git.ipfire.org Git - thirdparty/glibc.git/blame - malloc/tst-mcheck.c
malloc: Abort on heap corruption, without a backtrace [BZ #21754]
[thirdparty/glibc.git] / malloc / tst-mcheck.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2005-2017 Free Software Foundation, Inc.
bfc832cc
UD
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
bfc832cc
UD
18
19#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
e15f7de6 22#include <libc-diag.h>
bfc832cc
UD
23
24static int errors = 0;
25
26static void
27merror (const char *msg)
28{
29 ++errors;
30 printf ("Error: %s\n", msg);
31}
32
29955b5d
AS
33static int
34do_test (void)
bfc832cc
UD
35{
36 void *p, *q;
37
38 errno = 0;
39
3d7229c2
JM
40 DIAG_PUSH_NEEDS_COMMENT;
41#if __GNUC_PREREQ (7, 0)
42 /* GCC 7 warns about too-large allocations; here we want to test
43 that they fail. */
44 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
45#endif
bfc832cc 46 p = malloc (-1);
3d7229c2 47 DIAG_POP_NEEDS_COMMENT;
bfc832cc
UD
48
49 if (p != NULL)
50 merror ("malloc (-1) succeeded.");
51 else if (errno != ENOMEM)
52 merror ("errno is not set correctly.");
53
54 p = malloc (10);
55 if (p == NULL)
56 merror ("malloc (10) failed.");
57
58 p = realloc (p, 0);
59 if (p != NULL)
60 merror ("realloc (p, 0) failed.");
61
62 p = malloc (0);
63 if (p == NULL)
64 merror ("malloc (0) failed.");
65
66 p = realloc (p, 0);
67 if (p != NULL)
68 merror ("realloc (p, 0) failed.");
69
70 q = malloc (256);
71 if (q == NULL)
72 merror ("malloc (256) failed.");
73
74 p = malloc (512);
75 if (p == NULL)
76 merror ("malloc (512) failed.");
77
3d7229c2
JM
78 DIAG_PUSH_NEEDS_COMMENT;
79#if __GNUC_PREREQ (7, 0)
80 /* GCC 7 warns about too-large allocations; here we want to test
81 that they fail. */
82 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
83#endif
bfc832cc
UD
84 if (realloc (p, -256) != NULL)
85 merror ("realloc (p, -256) succeeded.");
86 else if (errno != ENOMEM)
87 merror ("errno is not set correctly.");
3d7229c2 88 DIAG_POP_NEEDS_COMMENT;
bfc832cc
UD
89
90 free (p);
91
92 p = malloc (512);
93 if (p == NULL)
94 merror ("malloc (512) failed.");
95
3d7229c2
JM
96 DIAG_PUSH_NEEDS_COMMENT;
97#if __GNUC_PREREQ (7, 0)
98 /* GCC 7 warns about too-large allocations; here we want to test
99 that they fail. */
100 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
101#endif
bfc832cc
UD
102 if (realloc (p, -1) != NULL)
103 merror ("realloc (p, -1) succeeded.");
104 else if (errno != ENOMEM)
105 merror ("errno is not set correctly.");
3d7229c2 106 DIAG_POP_NEEDS_COMMENT;
bfc832cc
UD
107
108 free (p);
109 free (q);
110
111 return errors != 0;
112}
29955b5d
AS
113
114#define TEST_FUNCTION do_test ()
115#include "../test-skeleton.c"