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

            Bug ID: 127566
           Summary: -Wnull-dereference not triggered in simple cases
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david at westcontrol dot com
  Target Milestone: ---

Test code :

int bad(int * p) {
    int a = *p;
    if (!p) return -1;
    return a;
}

People write code that references pointers before checking them for null.  They
should not do so, obviously, but mistakes happen.  gcc can delete what it sees
as unnecessary extra null pointer checks if "-fdelete-null-pointer-checks" is
enabled (as it is on many platforms with -O2 or higher).

To help catch these mistakes, there is the "-Wnull-derefence" warning that is
supposed to trigger in cases like this.  But it does not do so.  Compiling with 

    -O2 -Wall -Wextra -Wnull-dereference

happily gives code :

    bad:
        movl (%rdi), %eax
        ret

without any warnings or indication that the compiler has just deleted the null
pointer check.

Adding "-fanalyzer -Wanalyzer-null-dereference" does not help either.

I am a fan of optimising on the assumption that the programmer wrote correct
code.  But I am an even bigger fan of being told when I made a mistake, at
compile time rather than test time!
  • [Bug c/127566] New: -Wnull-deref... david at westcontrol dot com via Gcc-bugs

Reply via email to