diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2024-04-18 17:09:41 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2024-04-18 17:09:41 -0700 |
commit | 291feb3d6d70758185de71a21cf87d23a4b965cd (patch) | |
tree | da351457dee97ccde563caa70efe3d29bdf05f33 | |
parent | 792f80402ee06ce69bca3a8f2a84295999c3a170 (diff) |
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@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/lib/libxmu/-/merge_requests/16>
-rw-r--r-- | test/reallocarray.c | 14 |
1 files changed, 14 insertions, 0 deletions
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); } |