]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/121216 - ICE with VLA const string initializer
authorRichard Biener <rguenther@suse.de>
Tue, 22 Jul 2025 13:41:20 +0000 (15:41 +0200)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 23 Jul 2025 06:50:50 +0000 (08:50 +0200)
constant_byte_string fails to consider the string type might be VLA
when initialized by an empty string CTOR.

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
gcc/testsuite/gcc.dg/pr121216.c [new file with mode: 0644]

index ac4fdfaa2181e4d64444c5c37006939b4c70b85e..3f2b121ee038faa527e39f128f5a30a0ff380c5d 100644 (file)
@@ -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 (file)
index 0000000..a695b40
--- /dev/null
@@ -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);
+}