[Bug analyzer/98830] -Wanalyzer-null-argument on static_cast and inheritance

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 26 00:56:21 GMT 2021


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

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning in pr98646 was determined to be a false positive at least in part
because the cast was guarded by a test for the result of the call being
nonnull, like so:

  if (p->f ())
    static_cast<C*>(p->f ())->g ();          // bogus -Wanalyzer-null-argument

The analyzer diagnoses this too despite the test.  (Admittedly it's possible
for first call to f() to return nonnull and the second one null.)

But I would (and in pr96003.C did) consider it a false positive even without
the guard, because -Wnonnull isn't meant to trigger solely on the basis that a
pointer could be null (it has to be known to be null).  For example, both the
result of f() below may be null as well as A::g, but neither -Wnonnull nor
-Wanalyzer-null-argument triggers:

$ cat a.C && gcc  -O2 -S -Wall -fanalyzer a.C
struct A { void (*g)(void); };

A* f (void);

void g (void)
{
  f ()->g ();
}

If diagnosing the original test case is appropriate because p->f() may be null
then I would expect a warning for the call to f()->g() for the same reason.


More information about the Gcc-bugs mailing list