Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 701364 - sys-devel/gcc-9.2.0-r2 g++ empty string literal in a multidimensional array decays to a null pointer
Summary: sys-devel/gcc-9.2.0-r2 g++ empty string literal in a multidimensional array d...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard: wait for gcc-9.3.
Keywords:
Depends on:
Blocks:
 
Reported: 2019-11-27 22:27 UTC by William Breathitt Gray
Modified: 2020-03-29 20:12 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description William Breathitt Gray 2019-11-27 22:27:40 UTC
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-9.2.0-r2/work/gcc-9.2.0/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/9.2.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 9.2.0-r2 p3' --disable-esp --enable-libstdcxx-time --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-altivec --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libmudflap --disable-libssp --disable-systemtap --enable-vtable-verify --enable-lto --without-isl --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 9.2.0 (Gentoo 9.2.0-r2 p3)

---

Discovered on StackOverflow: https://stackoverflow.com/q/59076583/1806289

The following code produces an unexpected result in g++ (Gentoo 9.2.0-r2 p3) 9.2.0:

    #include <cstdio>

    const char *strArr[2][1] = { {"foo"}, {""}};

    int main(void) {
        printf("%p\t%p\n", strArr[0][0], strArr[1][0]);
        return 0;
    }

Compiling with `g++ -std=c++17 -pedantic test.cpp` and executing results in:

    $ ./a.out 
    0x55c61494d004  (nil)

The expected result is for `strArr[1][0]` to hold a proper address and not a null pointer.

This behavior is not present in the previous g++ 8.3.0 version, so I believe it is a regression in the current version.

Reproducible: Always
Comment 1 Sergei Trofimovich (RETIRED) gentoo-dev 2019-11-28 22:29:32 UTC
At a glance looks like a gcc bug.
Comment 2 Sergei Trofimovich (RETIRED) gentoo-dev 2019-11-28 22:40:00 UTC
Probably slightly more evident bug:

$ cat a.cc
/*
 * https://bugs.gentoo.org/701364
 *
 * $ g++ a.cc -o a && ./a
 * a1[0]    = 0x557f4d60e004
 * a2[0][0] = (nil)
 *
 * Here (nil) is entirely unexpected.
 */
#include <cstdio>

const char *a1[1] = {""};
const char *a2[1][1] = { {""} };

int main(void) {
    printf("a1[0]    = %p\n", a1[0]);
    printf("a2[0][0] = %p\n", a2[0][0]);
    return 0;
}

$ g++-9.2.0 a.cc -o a && ./a
a1[0]    = 0x5587a1b80004
a2[0][0] = (nil)
$ clang++-9 a.cc -o a && ./a
a1[0]    = 0x402012
a2[0][0] = 0x402012
Comment 3 Sergei Trofimovich (RETIRED) gentoo-dev 2019-11-28 23:14:25 UTC
Vanilla gcc from git master seems to work fine. Let's see if I can bisect it down to a fix.
Comment 4 Sergei Trofimovich (RETIRED) gentoo-dev 2019-11-29 08:28:31 UTC
Bisected master down to a https://gcc.gnu.org/PR90947 fix:

36154c6c9d44a8f4e8329c96f436ea47e89a18bc is the first bad commit
commit 36154c6c9d44a8f4e8329c96f436ea47e89a18bc
Author: msebor <msebor@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Thu Aug 1 23:45:36 2019 +0000

    PR c++/90947 - Simple lookup table of array of strings is miscompiled

    gcc/cp/ChangeLog:

            PR c++/90947
            * decl.c (reshape_init_array_1): Avoid truncating initializer
            lists containing string literals.

    gcc/testsuite/ChangeLog:

            PR c++/90947
            * c-c++-common/array-1.c: New test.
            * g++.dg/abi/mangle73.C: New test.
            * g++.dg/cpp2a/nontype-class23.C: New test.
            * g++.dg/init/array53.C: New test.

    gcc/ChangeLog:

            PR c++/90947
            * tree.c (type_initializer_zero_p): Define.
            * tree.h (type_initializer_zero_p): New function.



    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@273989 138bc75d-0d04-0410-961f-82ee72b054a4

 gcc/ChangeLog                                |   6 +
 gcc/cp/ChangeLog                             |   6 +
 gcc/cp/decl.c                                |   5 +-
 gcc/testsuite/ChangeLog                      |   8 +
 gcc/testsuite/c-c++-common/array-1.c         | 247 +++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/abi/mangle73.C          |  96 +++++++++++
 gcc/testsuite/g++.dg/cpp2a/nontype-class23.C | 102 +++++++++++
 gcc/testsuite/g++.dg/init/array53.C          |  33 ++++
 gcc/tree.c                                   |  67 ++++++++
 gcc/tree.h                                   |   6 +
 10 files changed, 574 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/array-1.c
 create mode 100644 gcc/testsuite/g++.dg/abi/mangle73.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class23.C
 create mode 100644 gcc/testsuite/g++.dg/init/array53.C
Comment 5 Sergei Trofimovich (RETIRED) gentoo-dev 2019-11-29 08:29:03 UTC
I suggest pulling the fix along with future gcc-9.3 release.
Comment 6 Sergei Trofimovich (RETIRED) gentoo-dev 2020-03-29 20:12:09 UTC
gcc-9.3.0 is in tree.