libXmu: Branch 'master'

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Apr 26 23:50:42 UTC 2024


 test/reallocarray.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)

New commits:
commit 291feb3d6d70758185de71a21cf87d23a4b965cd
Author: Alan Coopersmith <alan.coopersmith at oracle.com>
Date:   Thu Apr 18 17:09:41 2024 -0700

    test: Avoid incorrect -Wuse-after-free warning from gcc 13
    
    Workaround for gcc noticing that the test_nonfatal_assertions path through
    g_assert_null could return, thus leaving a possible path where we hadn't
    proved that p2 was NULL and thus p was safe to use, as discussed in:
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776
    
    Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
    Part-of: <https://gitlab.freedesktop.org/xorg/lib/libxmu/-/merge_requests/16>

diff --git a/test/reallocarray.c b/test/reallocarray.c
index 8a41905..99e8432 100644
--- a/test/reallocarray.c
+++ b/test/reallocarray.c
@@ -34,6 +34,7 @@
 #include <limits.h>
 #include <setjmp.h>
 #include <sys/resource.h>
+#include <assert.h>
 #ifdef HAVE_MALLOC_H
 # include <malloc.h>
 #endif
@@ -159,9 +160,16 @@ static void test_Xmureallocarray_oversize(void)
 
     p2 = Xmureallocarray(p, 2, ALLOC_LIMIT);
     g_assert_null(p2);
+    /* Unfortunately, g_assert_null has a test_nonfatal_assertions option that
+     * provides a code path that can get here even if p2 is not NULL, thus
+     * leading gcc to issue a -Wuse-after-free warning if we don't assert
+     * again that p2 is NULL and thus p is still valid.
+     */
+    assert(p2 == NULL);
     g_assert_cmpint(errno, ==, ENOMEM);
 
     errno = 0;
+    /* Free p, since we forced the realloc to fail, leaving it valid */
     free(p);
     g_assert_cmpint(errno, ==, 0);
 }
@@ -186,19 +194,25 @@ static void test_Xmureallocarray_overflow(void)
 
     p2 = Xmureallocarray(p, 1, SIZE_MAX);
     g_assert_null(p2);
+    /* See above about why we assert this again */
+    assert(p2 == NULL);
     g_assert_cmpint(errno, ==, ENOMEM);
 
     /* SQRT_SIZE_MAX * SQRT_SIZE_MAX == 0 due to overflow */
     p2 = Xmureallocarray(p, SQRT_SIZE_MAX, SQRT_SIZE_MAX);
     g_assert_null(p2);
+    assert(p2 == NULL);
     g_assert_cmpint(errno, ==, ENOMEM);
 
+
     /* Overflows to a small positive number */
     p2 = Xmureallocarray(p, SQRT_SIZE_MAX + 1, SQRT_SIZE_MAX);
     g_assert_null(p2);
+    assert(p2 == NULL);
     g_assert_cmpint(errno, ==, ENOMEM);
 
     errno = 0;
+    /* Free p, since we forced the reallocs to fail, leaving it valid */
     free(p);
     g_assert_cmpint(errno, ==, 0);
 }


More information about the xorg-commit mailing list