constant_byte_string fails to consider the string type might be VLA when initialized by an empty string CTOR.
Bootstrap and regtest running on x86_64-unknown-linux-gnu. PR middle-end/121216 * expr.cc (constant_byte_string): Check the string type size fits an uhwi before converting to uhwi. * gcc.dg/pr121216.c: New testcase. --- gcc/expr.cc | 2 ++ gcc/testsuite/gcc.dg/pr121216.c | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr121216.c diff --git a/gcc/expr.cc b/gcc/expr.cc index ac4fdfaa218..3f2b121ee03 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -13206,6 +13206,8 @@ constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl, of the expected type and size. */ if (!initsize) initsize = integer_zero_node; + else if (!tree_fits_uhwi_p (initsize)) + return NULL_TREE; unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize); if (size > (unsigned HOST_WIDE_INT) INT_MAX) diff --git a/gcc/testsuite/gcc.dg/pr121216.c b/gcc/testsuite/gcc.dg/pr121216.c new file mode 100644 index 00000000000..a695b40d12d --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr121216.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ + +int foo (void) +{ + const char *key = "obscurelevelofabstraction"; + const char reverse_key[__builtin_strlen(key)] = {'\0'}; /* { dg-error "variable-sized object may not be initialized except with an empty initializer" } */ + return __builtin_strlen(reverse_key); +} -- 2.43.0