Bug 94979 - gcc-9 generates incorrect code causing segfault
Summary: gcc-9 generates incorrect code causing segfault
Status: RESOLVED DUPLICATE of bug 91031
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 9.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-07 02:53 UTC by Alexey Makhalov
Modified: 2020-05-07 03:59 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey Makhalov 2020-05-07 02:53:15 UTC
I found this issue while compiling systemd-239 with gcc-9.3.0

The problem is in local data initialization on the stack such as array of pointers to global data.

-O0 is fine, but -01,02,03 eliminates such data causing app to segfault. 

Confirmed gcc-9 branch (up to commit 25c60fcadc397c42a0ec778e5b1238888f2c94d3)
still has this bug.


Test program is attached below.
Additional info and steps to reproduce:

~/gcc$ ./host-x86_64-pc-linux-gnu/gcc/xgcc -v
Using built-in specs.
COLLECT_GCC=./host-x86_64-pc-linux-gnu/gcc/xgcc
Target: x86_64-pc-linux-gnu
Configured with: ./configure --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c --disable-multilib --disable-bootstrap --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --with-system-zlib
Thread model: posix
gcc version 9.3.1 20200507 (GCC)
~/gcc$ ./host-x86_64-pc-linux-gnu/gcc/xgcc -B./host-x86_64-pc-linux-gnu/gcc/ test.c
~/gcc$ ./a.out
~/gcc$ echo $?
0
~/gcc$ ./host-x86_64-pc-linux-gnu/gcc/xgcc -B./host-x86_64-pc-linux-gnu/gcc/ -O1 test.c
~/gcc$ ./a.out
Segmentation fault (core dumped)


===================== test.c ==========================

#include <string.h>

int chararray(char **j) {
        if (!j)
                j = (char *[]){"a", "test"};

        return (strlen(j[0]) == 1) & (strlen(j[1]) == 4);
}

int main(void) {
        return chararray(0) == 0;
}

=======================================================
Comment 1 Andrew Pinski 2020-05-07 03:59:40 UTC
>    if (!j)
                j = (char *[]){"a", "test"};

The scope of the unnamed variable is just in that statement.
So you are using the unnamed variable outside of the scope.

See https://gcc.gnu.org/gcc-9/porting_to.html#complit

See PR 91031 also which is this is a dup of.

*** This bug has been marked as a duplicate of bug 91031 ***