https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90947

            Bug ID: 90947
           Summary: Simple lookup table of array of strings is miscompiled
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: maister at archlinux dot us
  Target Milestone: ---

On GCC 9.1.0, this snippet is miscompiled:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

static const char *vector_swizzle(int vecsize, int index)
{
        static const char *swizzle[4][4] = {
                { ".x", ".y", ".z", ".w" },
                { ".xy", ".yz", ".zw", nullptr },
                { ".xyz", ".yzw", nullptr, nullptr },
                { "", nullptr, nullptr, nullptr },
        };

        assert(vecsize >= 1 && vecsize <= 4);
        assert(index >= 0 && index < 4);
        assert(swizzle[vecsize - 1][index]);

        return swizzle[vecsize - 1][index];
}

int main(int argc, char **argv)
{
        puts(vector_swizzle(atoi(argv[1]), atoi(argv[2])));
}

Compiled with "g++ -o test test.cpp", and ran with ./test 4 0, and it asserts.
The last array entry of swizzle[3] are all nullptr for some reason. No other
compiler behaves like this.

Reply via email to