public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95473] New: GCC misses -Wnull-dereference warning in a simple code
@ 2020-06-02  5:39 haoxintu at gmail dot com
  2020-06-02  5:58 ` [Bug c++/95473] " marxin at gcc dot gnu.org
  2020-06-02 16:04 ` [Bug middle-end/95473] " msebor at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: haoxintu at gmail dot com @ 2020-06-02  5:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95473
           Summary: GCC misses -Wnull-dereference warning in a simple code
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

$cat null_ref.cc
int main(){
    int &x1 = *(int *)nullptr; // null reference
    return 0;
}

GCC can not catch any warning when I use -Wnull-dereference option. 
I also try to add -O1~Os, but GCC still produces nothing.

While in clang

$clang++-trunk -Wnull-dereference null_ref.cc 
null_ref.cc:2:15: warning: binding dereferenced null pointer to reference has
undefined behavior [-Wnull-dereference]
    int &x1 = *(int *)nullptr; // null reference
              ^~~~~~~~~~~~~~~
1 warning generated.

$g++ --version
g++ (GCC) 11.0.0 20200526 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

* [Bug c++/95473] GCC misses -Wnull-dereference warning in a simple code
  2020-06-02  5:39 [Bug c++/95473] New: GCC misses -Wnull-dereference warning in a simple code haoxintu at gmail dot com
@ 2020-06-02  5:58 ` marxin at gcc dot gnu.org
  2020-06-02 16:04 ` [Bug middle-end/95473] " msebor at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-06-02  5:58 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |law at gcc dot gnu.org,
                   |                            |manu at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
   Last reconfirmed|                            |2020-06-02
     Ever confirmed|0                           |1

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

* [Bug middle-end/95473] GCC misses -Wnull-dereference warning in a simple code
  2020-06-02  5:39 [Bug c++/95473] New: GCC misses -Wnull-dereference warning in a simple code haoxintu at gmail dot com
  2020-06-02  5:58 ` [Bug c++/95473] " marxin at gcc dot gnu.org
@ 2020-06-02 16:04 ` msebor at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-06-02 16:04 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |middle-end
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The reason for this is the same as in pr95461 comment #1.  Like -Warray-bounds,
for optimal results, -Wnull-dereference relies on optimization.  That includes
dead code elimination.  In the test case, since x1 is unused, it's eliminated
early on, well before the warning sees the code.

All these "late" warnings could also run earlier to detect more instances of
the problems, at the cost of issuing what most users view as false positives. 
Below is another example of such a trade-off.  Clang makes an effort to avoid
issuing these but a front end can only do that to a point.  To do much better
control and data flow analysis is required, which is what GCC does.

$ cat pr95473.c && clang -S -Wall -Wno-unused-variable -xc++ pr95473.c
void f ()
{ 
  if (0)
    int &x1 = *(int *)nullptr; // unreachable null reference (no warning)

}

void g ()
{
  const int i = 0;
  if (i)
    int &x1 = *(int *)nullptr; // unreachable null reference (no warning)

}

void h ()
{
  int i = 0;
  if (i)
    int &x1 = *(int *)nullptr; // unreachable null reference (warning)
}
pr95473.c:20:15: warning: binding dereferenced null pointer to reference has
      undefined behavior [-Wnull-dereference]
    int &x1 = *(int *)nullptr; // unreachable null reference (warning)
              ^~~~~~~~~~~~~~~
1 warning generated.

*** This bug has been marked as a duplicate of bug 95461 ***

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

end of thread, other threads:[~2020-06-02 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-02  5:39 [Bug c++/95473] New: GCC misses -Wnull-dereference warning in a simple code haoxintu at gmail dot com
2020-06-02  5:58 ` [Bug c++/95473] " marxin at gcc dot gnu.org
2020-06-02 16:04 ` [Bug middle-end/95473] " msebor at gcc dot gnu.org

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).