[Bug c/102559] New: missing warning comparing result of a nonnull function to null

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Oct 1 16:59:07 GMT 2021


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

            Bug ID: 102559
           Summary: missing warning comparing result of a nonnull function
                    to null
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC folds equality comparisons of nonnull function and null to zero but it
fails to issue any warning for doing so.  In contrast, comparing the value of a
pointer parameter and null in a nonnull function triggers a -Wnonnull-compare.

Clang is more consistent and issues warnings for all such (likely) misues.

$ cat z.c && gcc -O -S -Wall -fdump-tree-optimized=/dev/stdout z.c
__attribute__ ((returns_nonnull)) void* f (void);

int g (void)
{
  return f () == 0;   // missing warning (return folded to zero)
}

int h (void *p, const void *q, int n)
{
  return !__builtin_memcpy (p, q, n);   // missing warning (folded to zero)
}

;; Function g (g, funcdef_no=0, decl_uid=1980, cgraph_uid=1, symbol_order=0)

int g ()
{
  <bb 2> [local count: 1073741824]:
  f ();
  return 0;

}



;; Function h (h, funcdef_no=1, decl_uid=1985, cgraph_uid=2, symbol_order=1)

int h (void * p, const void * q, int n)
{
  long unsigned int _1;

  <bb 2> [local count: 1073741824]:
  _1 = (long unsigned int) n_4(D);
  __builtin_memcpy (p_6(D), q_7(D), _1);
  return 0;

}


More information about the Gcc-bugs mailing list