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

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Okay, here's a slightly less contrived test case:

  struct A { int a [1]; };
  struct B { int b [2]; };

  A foo ();
  B bar (); 

  int *p = foo ().a;
  int *q = bar ().b;

This test case should also illustrate that diagnosing this code is useful
regardless of whether the pointers are used (or rather, regardless of whether
GCC can prove whether or not they are used).  Augmenting the code above with:

  bool b = p == q;

shows that even at -O2, b is dynamically initialized by comparing the values of
the dangling pointers.  Since the comparison as well as any other use of the
pointers is undefined, the initialization expressions for the pointers deserve
a warning.

In contrast to the invalid code above, the following initialization is
well-defined:

  bool b = foo ().a == bar ().b;

but it also deserves a warning because it will never evaluate to true and is
likely a coding mistake.  GCC recognizes this and folds the initialization into
a false, though it doesn't warn and still assigns it to b dynamically.

Reply via email to