From eb4f5446895b5c532fd89827bfae4ea10fcaee31 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 1 Jul 2025 04:26:20 +0200 Subject: [PATCH] malloctest: Fix issue with use after free Do not call realloc() with a previously freed pointer. Perform a successful reallocation. --- testsuites/libtests/malloctest/init.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/testsuites/libtests/malloctest/init.c b/testsuites/libtests/malloctest/init.c index d72f49112e..cf200b5c34 100644 --- a/testsuites/libtests/malloctest/init.c +++ b/testsuites/libtests/malloctest/init.c @@ -1543,6 +1543,7 @@ static void test_early_malloc( void ) void *r; void *s; void *t; + void *u; p = malloc( 1 ); rtems_test_assert( p != NULL ); @@ -1556,15 +1557,8 @@ static void test_early_malloc( void ) free( q ); -/* - * This was added to address the following warning. - * warning: pointer 'q' used after 'free' - */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wuse-after-free" - r = realloc( q, 128 ); - rtems_test_assert( r == q ); -#pragma GCC diagnostic pop + r = realloc( NULL, 128 ); + rtems_test_assert( r != q ); s = malloc( 1 ); rtems_test_assert( s != NULL ); @@ -1575,7 +1569,10 @@ static void test_early_malloc( void ) rtems_test_assert( t != NULL ); rtems_test_assert( t != r ); - free( t ); + u = realloc( t, 384 ); + rtems_test_assert( t == u ); + + free( u ); } RTEMS_SYSINIT_ITEM(