Bug 102103 - missing warning comparing array address to null
Summary: missing warning comparing array address to null
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 12.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2021-08-27 19:21 UTC by Martin Sebor
Modified: 2021-10-21 05:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 10.2.0, 11.2.0, 12.0, 4.5.3, 4.6.4, 4.9.4, 5.5.0, 6.4.0, 7.2.0, 8.3.0, 9.1.0
Last reconfirmed: 2021-08-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2021-08-27 19:21:37 UTC
GCC issues -Waddress only for the equality test in f() but not those in g() or h(), even though all three evaluate to false (and even though GCC folds all three to false even with no optimization).  It would be helpful to diagnose all three.

$ cat z.c && gcc -S -Wall -fdump-tree-optimized=/dev/stdout z.c
void f (void)
{ 
  int i;
  if (&i == 0)   // -Waddress (good)
    __builtin_abort ();
}

void g (void)
{
  int a[1];
  if (a == 0)   // missing warning
    __builtin_abort ();
}

void h (void)
{
  int a[1][2];
  if (a[0] == 0)   // missing warning
    __builtin_abort ();
}

z.c: In function ‘f’:
z.c:4:10: warning: the comparison will always evaluate as ‘false’ for the address of ‘i’ will never be NULL [-Waddress]
    4 |   if (&i == 0)   // -Waddress (good)
      |          ^~

;; Function f (f, funcdef_no=0, decl_uid=1943, cgraph_uid=1, symbol_order=0)

void f ()
{
  int i;

  <bb 2> :
  i ={v} {CLOBBER};
  return;

}



;; Function g (g, funcdef_no=1, decl_uid=1947, cgraph_uid=2, symbol_order=1)

void g ()
{
  int a[1];

  <bb 2> :
  a ={v} {CLOBBER};
  return;

}



;; Function h (h, funcdef_no=2, decl_uid=1951, cgraph_uid=3, symbol_order=2)

void h ()
{
  int a[1][2];

  <bb 2> :
  a ={v} {CLOBBER};
  return;

}


Clang diagnoses both the one in f() and in g() (but not the one in h()).  GCC 4.4.7 does as well but the latter has been lost since GCC 4.5  (see https://gcc.godbolt.org/z/8dbjxv89E) so technically this could be viewed as a regression.
Comment 1 Andrew Pinski 2021-08-27 19:23:59 UTC
Related to PR 33925.
Comment 3 CVS Commits 2021-10-01 17:57:47 UTC
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:4dc7ce6fb3917958d1a6036d8acf2953b9c1b868

commit r12-4059-g4dc7ce6fb3917958d1a6036d8acf2953b9c1b868
Author: Martin Sebor <msebor@redhat.com>
Date:   Fri Oct 1 11:50:25 2021 -0600

    Enhance -Waddress to detect more suspicious expressions [PR102103].
    
    Resolves:
    PR c/102103 - missing warning comparing array address to null
    
    gcc/ChangeLog:
    
            PR c/102103
            * doc/invoke.texi (-Waddress): Update.
            * gengtype.c (write_types): Avoid -Waddress.
            * poly-int.h (POLY_SET_COEFF): Avoid using null.
    
    gcc/c-family/ChangeLog:
    
            PR c/102103
            * c-common.c (decl_with_nonnull_addr_p): Handle members.
            Check and perform warning suppression.
            (c_common_truthvalue_conversion): Enhance warning suppression.
    
    gcc/c/ChangeLog:
    
            PR c/102103
            * c-typeck.c (maybe_warn_for_null_address): New function.
            (build_binary_op): Call it.
    
    gcc/cp/ChangeLog:
    
            PR c/102103
            * typeck.c (warn_for_null_address): Enhance.
            (cp_build_binary_op): Call it also for member pointers.
    
    gcc/fortran/ChangeLog:
    
            PR c/102103
            * array.c: Remove an unnecessary test.
            * trans-array.c: Same.
    
    gcc/testsuite/ChangeLog:
    
            PR c/102103
            * g++.dg/cpp0x/constexpr-array-ptr10.C: Suppress a valid warning.
            * g++.dg/warn/Wreturn-local-addr-6.C: Correct a cast.
            * gcc.dg/Waddress.c: Expect a warning.
            * c-c++-common/Waddress-3.c: New test.
            * c-c++-common/Waddress-4.c: New test.
            * g++.dg/warn/Waddress-5.C: New test.
            * g++.dg/warn/Waddress-6.C: New test.
            * g++.dg/warn/pr101219.C: Expect a warning.
            * gcc.dg/Waddress-3.c: New test.
Comment 4 Martin Sebor 2021-10-01 18:00:11 UTC
Done for GCC 12.
Comment 5 Eric Gallager 2021-10-02 09:10:13 UTC
This should probably get a note in gcc-12/changes.html IMO