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

            Bug ID: 71699
           Summary: bogus -Wmaybe-uninitialized warning: gcc misses that
                    non-NULL pointer + offset can never be NULL
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: palves at redhat dot com
  Target Milestone: ---

gcc does not understand that adding an offset to a pointer returned by a
returns_nonnull function can never yield a NULL pointer.  Vis:

$ cat test.c 
char *xstrdup (const char *) __attribute__ ((__returns_nonnull__));

#define PREFIX "some "

int
main ()
{
  char *saveptr;
  char *name = xstrdup (PREFIX "name");

  // name = PREFIX "name";              // this makes the warning go away

  char *tail = name + sizeof (PREFIX) - 1;
  // tail = &name[sizeof (PREFIX) - 1]; // this does not help
  // tail = name;                       // while this makes the warning go away

  if (tail == 0)
    tail = saveptr;
  while (*tail == ' ')
    ++tail;

  return 0;
}
$ /opt/gcc/bin/gcc test.c -c -Wall
test.c: In function ‘main’:
test.c:18:10: warning: ‘saveptr’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
     tail = saveptr;
     ~~~~~^~~~~~~~~

Enabling optimization does not make it go away:

$ /opt/gcc/bin/gcc -O2 test.c -c -Wall
test.c: In function ‘main’:
test.c:19:10: warning: ‘saveptr’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
   while (*tail == ' ')
          ^~~~~

That was gcc version 7.0.0 20160503 (experimental) built from sources.

Fedora 23's gcc 5.3.1 shows the same.

This is a reduced testcase based on a warning gcc issued when building gdb:
 https://sourceware.org/ml/gdb-patches/2016-06/msg00515.html

Reply via email to