Bug 1478864 - gcc7 triggers false-positive warnings
gcc7 triggers false-positive warnings
Status: CLOSED CURRENTRELEASE
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
26
All Linux
unspecified Severity unspecified
: ---
: ---
Assigned To: Jakub Jelinek
Fedora Extras Quality Assurance
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2017-08-07 07:01 EDT by Greg Kurz
Modified: 2018-04-27 18:35 EDT (History)
7 users (show)

See Also:
Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2018-04-27 18:35:16 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)


External Trackers
Tracker ID Priority Status Summary Last Updated
GNU Compiler Collection 81782 None None None 2017-08-09 08:21 EDT

  None (edit)
Description Greg Kurz 2017-08-07 07:01:37 EDT
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 08:08:48 EDT
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 08:21:11 EDT
Thanks for the report, I've opened PR81782.
Comment 3 Marek Polacek 2017-12-08 09:49:57 EST
This is now fixed on trunk.
Comment 4 Marek Polacek 2018-04-27 18:35:16 EDT
gcc-7.3.1-5.fc27.x86_64 doesn't warn anymore, closing.

Note You need to log in before you can comment on or make changes to this bug.