public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null()
@ 2024-04-18 23:29 alan.coopersmith at oracle dot com
  2024-04-18 23:39 ` [Bug tree-optimization/114776] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: alan.coopersmith at oracle dot com @ 2024-04-18 23:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

            Bug ID: 114776
           Summary: -Wuse-after-free analysis believes assert() but not
                    g_assert_null()
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alan.coopersmith at oracle dot com
  Target Milestone: ---

Created attachment 57985
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57985&action=edit
Test case

When reviewing the -Wuse-after-free error reported in:
 https://gitlab.freedesktop.org/xorg/lib/libxmu/-/merge_requests/13

in which gcc gives a use-after-free warning for p in:
    p2 = Xmureallocarray(p, 2, ALLOC_LIMIT);
    g_assert_null(p2);
    [...]
    free(p);
even though the realloc failed and returned NULL and did not free p.

I found that the -Wuse-after-free warning went away if I changed
    g_assert_null(p2);
to
    assert(p2 == NULL);

so it appears something in the way g_assert_null from glib is defined
does not convince gcc that p2 must be NULL the way the simple system
assert does.

I've made a cut down test case, and can reproduce the false alarm with

gcc `pkg-config --cflags glib-2.0` -Wall -O2 -c realloc.c

using gcc 13.2.0 on Solaris 11.4.66 on x86-64.  (The original report came
from a gentoo Linux user, and I reproduced on Debian Linux, so this isn't
OS-specific.)

Adding -DUSE_SYSTEM_ASSERT to the compiler command line makes the warning
go away.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
@ 2024-04-18 23:39 ` pinskia at gcc dot gnu.org
  2024-04-18 23:43 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18 23:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2024-04-18
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Can you attach the preprocessed source?

It is almost definitely the way g_assert_null is defined.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
  2024-04-18 23:39 ` [Bug tree-optimization/114776] " pinskia at gcc dot gnu.org
@ 2024-04-18 23:43 ` pinskia at gcc dot gnu.org
  2024-04-18 23:44 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18 23:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
As far as I can tell g_assertion_message does not have noreturn on it which
means this invalid.

It only has G_ANALYZER_NORETURN on it.
which is only defined to analyzer_noreturn if running under clang's analyzer.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
  2024-04-18 23:39 ` [Bug tree-optimization/114776] " pinskia at gcc dot gnu.org
  2024-04-18 23:43 ` pinskia at gcc dot gnu.org
@ 2024-04-18 23:44 ` pinskia at gcc dot gnu.org
  2024-04-18 23:50 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18 23:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Seems like g_assertion_message should have _Noreturn on it if you are compiling
for C11-C17 and [[noreturn]] for C++11+ (and C23+).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
                   ` (2 preceding siblings ...)
  2024-04-18 23:44 ` pinskia at gcc dot gnu.org
@ 2024-04-18 23:50 ` pinskia at gcc dot gnu.org
  2024-04-19  0:00 ` alan.coopersmith at oracle dot com
  2024-04-19  0:03 ` alan.coopersmith at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18 23:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---


So g_assertion_message can actually return in one case.
https://github.com/GNOME/glib/blob/81eaabb30803b0e30edca888772f7459fa389650/glib/gtestutils.c#L3305

So yes this is a valid warning.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
                   ` (3 preceding siblings ...)
  2024-04-18 23:50 ` pinskia at gcc dot gnu.org
@ 2024-04-19  0:00 ` alan.coopersmith at oracle dot com
  2024-04-19  0:03 ` alan.coopersmith at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: alan.coopersmith at oracle dot com @ 2024-04-19  0:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

--- Comment #5 from Alan Coopersmith <alan.coopersmith at oracle dot com> ---
Created attachment 57986
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57986&action=edit
Preproccessed test case

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/114776] -Wuse-after-free analysis believes assert() but not g_assert_null()
  2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
                   ` (4 preceding siblings ...)
  2024-04-19  0:00 ` alan.coopersmith at oracle dot com
@ 2024-04-19  0:03 ` alan.coopersmith at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: alan.coopersmith at oracle dot com @ 2024-04-19  0:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114776

--- Comment #6 from Alan Coopersmith <alan.coopersmith at oracle dot com> ---
Thanks, I didn't realize there was a test_nonfatal_assertions path through
the g_assert that could return here.

I'll update the wording on my proposed workaround to reflect that:
https://gitlab.freedesktop.org/xorg/lib/libxmu/-/merge_requests/16

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-19  0:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 23:29 [Bug c/114776] New: -Wuse-after-free analysis believes assert() but not g_assert_null() alan.coopersmith at oracle dot com
2024-04-18 23:39 ` [Bug tree-optimization/114776] " pinskia at gcc dot gnu.org
2024-04-18 23:43 ` pinskia at gcc dot gnu.org
2024-04-18 23:44 ` pinskia at gcc dot gnu.org
2024-04-18 23:50 ` pinskia at gcc dot gnu.org
2024-04-19  0:00 ` alan.coopersmith at oracle dot com
2024-04-19  0:03 ` alan.coopersmith at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).