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

            Bug ID: 102381
           Summary: unexpected -Wmaybe-uninitialized
           Product: gcc
           Version: 7.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hv at crypt dot org
  Target Milestone: ---

This is reduced from perl source code. Reduction was a challenge, so there's a
risk the essence may have been lost.

The following code gives a -Wmaybe-uninitialized warning with each of "gcc-7
(Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0", "gcc-9 (Ubuntu 9.2.1-17ubuntu1~18.04.1)
9.2.1 20191102" and self-built "gcc (GCC) 11.2.0".

% cat test.c
extern void fail(void) __attribute__((__noreturn__));
int** f(void);

extern int *ip;
extern int i2, i3; 

void test(int i1) {
    int **ipp;

    if (i1) {
        ipp = f();
    }
    for (int i4 = 0; i4 < i2; i4++) {
        if (((i3 & 1) != 1) && ((i3 & 3) != 3))
            fail();
    }
    if (i1) {
        ip = *ipp;
    }
    return;
}
% gcc -c -Wmaybe-uninitialized -O1 test.c
test.c: In function 'test':
test.c:18:14: warning: 'ipp' may be used uninitialized in this function
[-Wmaybe-uninitialized]
         ip = *ipp;
              ^~~~
% 

I think it should be clear that i1 does not change within this code, and the
'ip = *ipp' therefore cannot be reached without also previously having reached
'ipp = f()'. The sensitivity of the intervening code suggests this must be a
bug, eg replacing 'if (((i3 & 1) != 1) && ((i3 & 3) != 3))' with 'if ((i3 & 3)
!= 3)' makes the warning disappear.

Reply via email to