Bug 1478864

Summary: gcc7 triggers false-positive warnings
Product: [Fedora] Fedora Reporter: Greg Kurz <gkurz>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED CURRENTRELEASE QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 26CC: davejohansen, eblake, fweimer, jakub, jwakely, law, mpolacek
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-04-27 22:35:16 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Greg Kurz 2017-08-07 11:01:37 UTC
Description of problem:

The following code snippet spits a warning with gcc-7.1.1-3.fc26 and -Wall on
ppc64le AND x86_64, while we would expect the compiler to optimize the loop
away since the array is empty.

int foo(void)
{
    char empty_array[] = { };
    int i, ret = 0;

    for (i = 0; i < (int) (sizeof(empty_array) / sizeof(empty_array[0])); i++) {
        ret = empty_array[i];
    }

    return ret;
}

Version-Release number of selected component (if applicable):

gcc-7.1.1-3.fc26

How reproducible:

Always.

Steps to Reproduce:

1. cat>foo.c<<EOF
int foo(void)
{
    char empty_array[] = { };
    int i, ret = 0;

    for (i = 0; i < (int) (sizeof(empty_array) / sizeof(empty_array[0])); i++) {
        ret = empty_array[i];
    }

    return ret;
}
EOF
2. gcc -c -Wall foo.c
3.

Actual results:

foo.c: In function ‘foo’:
foo.c:7:26: warning: ‘empty_array[i]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         ret = empty_array[i];

Expected results:

No warning.

Additional info:

This doesn't happen on fedora24 (gcc-6.3.1-1.fc24) or fedora25 (gcc-6.4.1-1.fc25).

This code pattern is used in QEMU, and causes the build to fail because we pass -Wall -Werror to gcc by default.

Comment 1 Eric Blake 2017-08-07 12:08:48 UTC
Also note that removing the '(int)' cast is enough to silence the warning; no idea why the cast is confusing the compiler.

Comment 2 Marek Polacek 2017-08-09 12:21:11 UTC
Thanks for the report, I've opened PR81782.

Comment 3 Marek Polacek 2017-12-08 14:49:57 UTC
This is now fixed on trunk.

Comment 4 Marek Polacek 2018-04-27 22:35:16 UTC
gcc-7.3.1-5.fc27.x86_64 doesn't warn anymore, closing.