Martin Sebor <msebor at gcc dot> changed:

           What    |Removed                     |Added
           Keywords|                            |diagnostic
            Summary|-Wnull-dereference doesn't  |missing -Wnull-dereference
                   |work with LTO               |on a variable null array
                   |                            |reference with LTO

--- Comment #1 from Martin Sebor <msebor at gcc dot> ---
With more testing I see that the -Wnull-dereference option actually does work
with LTO, it's just prone to false negatives.  Below is a variant of the test
case for -Wnonnull.  It triggers -Wnull-dereference for the constant zero index
but it doesn't trigger -Wnonnull.

$ (set -x; cat a.c; cc='gcc -O3 -Wall -Wextra -Wnull-dereference -flto'; $cc -c
a.c && $cc -DMAIN -c -o main.o a.c && $cc a.o main.o)
+ cat a.c
#if MAIN

extern int f (const char*);

int main (void)
  char *s = 0;
  return f (s);


static int __attribute__ ((nonnull (1)))
g (const char *s)
  return __builtin_strlen (s);

int f (const char *s)
  return s[0] ? g (s) : 0;   // -Wnull-dereference here (good)

+ cc='gcc -O3 -Wall -Wextra -Wnull-dereference -flto'
+ gcc -O3 -Wall -Wextra -Wnull-dereference -flto -c a.c
+ gcc -O3 -Wall -Wextra -Wnull-dereference -flto -DMAIN -c -o main.o a.c
+ gcc -O3 -Wall -Wextra -Wnull-dereference -flto a.o main.o
a.c: In function ‘main’:
a.c:21:11: warning: null pointer dereference [-Wnull-dereference]
   return s[0] ? g (s) : 0;

Reply via email to