mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-26 04:31:03 +00:00
malloc: Exit early on test failure in tst-realloc
This addresses more (correct) use-after-free warnings reported by
GCC 12 on some targets.
Fixes commit c094c232eb
("Avoid
-Wuse-after-free in tests [BZ #26779].").
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
This commit is contained in:
parent
590f5992b6
commit
d653fd2d9e
@ -20,15 +20,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <libc-diag.h>
|
#include <libc-diag.h>
|
||||||
|
#include <support/check.h>
|
||||||
static int errors = 0;
|
|
||||||
|
|
||||||
static void
|
|
||||||
merror (const char *msg)
|
|
||||||
{
|
|
||||||
++errors;
|
|
||||||
printf ("Error: %s\n", msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_test (void)
|
do_test (void)
|
||||||
@ -51,11 +43,11 @@ do_test (void)
|
|||||||
save = errno;
|
save = errno;
|
||||||
|
|
||||||
if (p != NULL)
|
if (p != NULL)
|
||||||
merror ("realloc (NULL, -1) succeeded.");
|
FAIL_EXIT1 ("realloc (NULL, -1) succeeded.");
|
||||||
|
|
||||||
/* errno should be set to ENOMEM on failure (POSIX). */
|
/* errno should be set to ENOMEM on failure (POSIX). */
|
||||||
if (p == NULL && save != ENOMEM)
|
if (p == NULL && save != ENOMEM)
|
||||||
merror ("errno is not set correctly");
|
FAIL_EXIT1 ("errno is not set correctly");
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
@ -64,18 +56,18 @@ do_test (void)
|
|||||||
save = errno;
|
save = errno;
|
||||||
|
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("realloc (NULL, 10) failed.");
|
FAIL_EXIT1 ("realloc (NULL, 10) failed.");
|
||||||
|
|
||||||
free (p);
|
free (p);
|
||||||
|
|
||||||
p = calloc (20, 1);
|
p = calloc (20, 1);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("calloc (20, 1) failed.");
|
FAIL_EXIT1 ("calloc (20, 1) failed.");
|
||||||
|
|
||||||
/* Check increasing size preserves contents (C89). */
|
/* Check increasing size preserves contents (C89). */
|
||||||
p = realloc (p, 200);
|
p = realloc (p, 200);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("realloc (p, 200) failed.");
|
FAIL_EXIT1 ("realloc (p, 200) failed.");
|
||||||
|
|
||||||
c = p;
|
c = p;
|
||||||
ok = 1;
|
ok = 1;
|
||||||
@ -87,20 +79,20 @@ do_test (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ok == 0)
|
if (ok == 0)
|
||||||
merror ("first 20 bytes were not cleared");
|
FAIL_EXIT1 ("first 20 bytes were not cleared");
|
||||||
|
|
||||||
free (p);
|
free (p);
|
||||||
|
|
||||||
p = realloc (NULL, 100);
|
p = realloc (NULL, 100);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("realloc (NULL, 100) failed.");
|
FAIL_EXIT1 ("realloc (NULL, 100) failed.");
|
||||||
|
|
||||||
memset (p, 0xff, 100);
|
memset (p, 0xff, 100);
|
||||||
|
|
||||||
/* Check decreasing size preserves contents (C89). */
|
/* Check decreasing size preserves contents (C89). */
|
||||||
p = realloc (p, 16);
|
p = realloc (p, 16);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("realloc (p, 16) failed.");
|
FAIL_EXIT1 ("realloc (p, 16) failed.");
|
||||||
|
|
||||||
c = p;
|
c = p;
|
||||||
ok = 1;
|
ok = 1;
|
||||||
@ -112,7 +104,7 @@ do_test (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ok == 0)
|
if (ok == 0)
|
||||||
merror ("first 16 bytes were not correct");
|
FAIL_EXIT1 ("first 16 bytes were not correct");
|
||||||
|
|
||||||
/* Check failed realloc leaves original untouched (C89). */
|
/* Check failed realloc leaves original untouched (C89). */
|
||||||
DIAG_PUSH_NEEDS_COMMENT;
|
DIAG_PUSH_NEEDS_COMMENT;
|
||||||
@ -124,7 +116,7 @@ do_test (void)
|
|||||||
c = realloc (p, -1);
|
c = realloc (p, -1);
|
||||||
DIAG_POP_NEEDS_COMMENT;
|
DIAG_POP_NEEDS_COMMENT;
|
||||||
if (c != NULL)
|
if (c != NULL)
|
||||||
merror ("realloc (p, -1) succeeded.");
|
FAIL_EXIT1 ("realloc (p, -1) succeeded.");
|
||||||
|
|
||||||
c = p;
|
c = p;
|
||||||
ok = 1;
|
ok = 1;
|
||||||
@ -136,29 +128,21 @@ do_test (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ok == 0)
|
if (ok == 0)
|
||||||
merror ("first 16 bytes were not correct after failed realloc");
|
FAIL_EXIT1 ("first 16 bytes were not correct after failed realloc");
|
||||||
|
|
||||||
#if __GNUC_PREREQ (12, 0)
|
|
||||||
/* Ignore a valid warning about using a pointer made indeterminate
|
|
||||||
by a prior call to realloc(). */
|
|
||||||
DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
|
|
||||||
#endif
|
|
||||||
/* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
|
/* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
|
||||||
p = realloc (p, 0);
|
p = realloc (p, 0);
|
||||||
#if __GNUC_PREREQ (12, 0)
|
|
||||||
DIAG_POP_NEEDS_COMMENT;
|
|
||||||
#endif
|
|
||||||
if (p != NULL)
|
if (p != NULL)
|
||||||
merror ("realloc (p, 0) returned non-NULL.");
|
FAIL_EXIT1 ("realloc (p, 0) returned non-NULL.");
|
||||||
|
|
||||||
/* realloc (NULL, 0) acts like malloc (0) (glibc). */
|
/* realloc (NULL, 0) acts like malloc (0) (glibc). */
|
||||||
p = realloc (NULL, 0);
|
p = realloc (NULL, 0);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
merror ("realloc (NULL, 0) returned NULL.");
|
FAIL_EXIT1 ("realloc (NULL, 0) returned NULL.");
|
||||||
|
|
||||||
free (p);
|
free (p);
|
||||||
|
|
||||||
return errors != 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TEST_FUNCTION do_test ()
|
#define TEST_FUNCTION do_test ()
|
||||||
|
Loading…
Reference in New Issue
Block a user