Bug 1478864 - gcc7 triggers false-positive warnings
Summary: gcc7 triggers false-positive warnings
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 26
Hardware: All
OS: Linux
unspecified
unspecified
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2017-08-07 11:01 UTC by Greg Kurz
Modified: 2018-04-27 22:35 UTC (History)
7 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-04-27 22:35:16 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
GNU Compiler Collection 81782 0 None None None 2017-08-09 12:21:10 UTC

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.


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