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

            Bug ID: 89230
           Summary: Bogus uninited usage warning
           Product: gcc
           Version: 7.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lavr at ncbi dot nlm.nih.gov
  Target Milestone: ---

I have a piece of code that reads like this (line numbers added):

   2401                 if (d->D_fid[2]  ||  !(hdr = getfh(vol, d->D_fid[0],
tmp))  ||  d->D_fid[1] != hdr->H_fseq) {
   2402                     char temp[80];
   2403                     sprintf(temp, "%s.%s;%hu", name, type, d->D_fver);
   2404                     printf("%s%-20s ", pfx, temp);
   2405                     sprintf(temp, "(%ho,%ho,%ho)",  d->D_fid[0],
d->D_fid[1], d->D_fid[2]);
   2406                     printf("%-22s ", temp);
   2407                     if (d->D_fid[2])
   2408                         printf("file header is on volume %hu.\n",
d->D_fid[2]);
   2409                     else if (!hdr)
   2410                         printf("unable to get file header\n");
   2411                     else
   2412                         printf("stale sequence no. (%ho)\n",
hdr->H_fseq);
   2413                 }

In the above "d" is a local variable (a pointer to a structure), unrelated to
any of the other variables shown in the fragment above (that is, not pointing
into any areas occupied by "vol" and "tmp" that are passed to "getfh()", hence,
cannot be indirectly modified by that call).  When GCC compiles the code
optimized (-O6), it gives out the following warning:

rsx11io.c: In function ‘list_dir.constprop’:
rsx11io.c:2409:29: warning: ‘hdr’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
                     else if (!hdr)
                             ^
Note that the warning is completely uncalled for, because of the "if" on line
2401, checking first whether "d->D_fid[2]" is non-zero, and if not, then
proceeding with the assignment of "hdr".  Now, line 2409 could only be reached
if "d->D_fid[2]" was zero, meaning "hdr" was in fact initialized.

Unfortunately, I failed to reduce the above to a test case, as simplifying the
code makes the warning disappear somehow.

Moreover, the warning also disappears if I remove just the lines 2402-2407 from
the original source code, so that the outer "if" is followed by the inner "if"
immediately.

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with:
/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0/configure
--srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0
--prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc
--docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C
--build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin
--without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib
--enable-shared --enable-shared-libgcc --enable-static
--enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit
--with-dwarf2 --with-tune=generic
--enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite
--enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp
--enable-libitm --enable-libquadmath --enable-libquadmath-support
--disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as
--with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix
--without-libintl-prefix --with-system-zlib --enable-linker-build-id
--with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 7.4.0 (GCC)

Reply via email to