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

            Bug ID: 84463
           Summary: Supposedly-incompliant "error: '* key0' is not a
                    constant expression"
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sergey.ignatchenko at ithare dot com
  Target Milestone: ---

== COMPILER ==========

root@ubuntu-gcc7:~/ithare/kscope/test/nix# g++ --version
g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

== CODE ============

#include <stdint.h>
#include <cstddef>

struct chacha_tv {
        const char *desc;
        const uint8_t key[32];
        const uint8_t iv[8];
        const size_t len;
        const unsigned char out[512];
};

static constexpr chacha_tv chacha_test_vectors[] = {
        {
                "TC1: All zero key and IV",
                {
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                },
                {
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                },
                64,
                {
                        0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
                        0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
                        0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
                        0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
                        0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
                        0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
                        0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
                        0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
                },
        },
};

constexpr int
KSCOPE_CT_Chacha_set_key_iv2(const uint8_t* key0, int keybits /*128 or 256*/,
const uint8_t iv0[8], const uint8_t counter0[8]) {
        uint8_t c = key0[0];
        return 0;
}

constexpr static const chacha_tv* tv = &chacha_test_vectors[0];
constexpr static const int k = tv->key[0];
constexpr static uint8_t kk[3] = {0,1,2};
constexpr static int x0 =
KSCOPE_CT_Chacha_set_key_iv2(chacha_test_vectors[0].key,256,tv->iv,nullptr);//OK
constexpr static int x1 =
KSCOPE_CT_Chacha_set_key_iv2(kk,256,tv->iv,nullptr);//OK
constexpr static int x2 =
KSCOPE_CT_Chacha_set_key_iv2(tv->key,256,tv->iv,nullptr);

== PROBLEM ============

When trying to compile the code above with GCC 7.2, the following error is
generated:
../chachatest.cpp:50:55:   in constexpr expansion of
'KSCOPE_CT_Chacha_set_key_i
v2(((const uint8_t*)(((const uint8_t (*)[32])tv) + 8)), 256, ((const
uint8_t*)((
(const uint8_t (*)[8])tv) + 40)), 0)'
../chachatest.cpp:41:27: error: '* key0' is not a constant expression
         uint8_t c = key0[0];

Expected behaviour is to acknowledge that key0 is an 'address constant
expression' in a sense of 5.19 (at least my humble reading of the standard says
that it is), and to allow the code to be compiled. Notes:
- note that both x0 and x1 compile ok
- both Clang and MSVC seem to agree it is an 'address constant expression' and
don't complain.

Reply via email to