]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fix infinite looping with arr[arr] [PR125454]
authorMarek Polacek <polacek@redhat.com>
Thu, 28 May 2026 17:43:58 +0000 (13:43 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 28 May 2026 19:44:19 +0000 (15:44 -0400)
Here r16-3466 moved the canonicalization step that transforms
idx[array] to array[idx] to the beginning of cp_build_array_ref.
When we have array[array], we'll be swapping till we blow the stack.

Previously, we'd give the !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
error so there was no problem.

PR c++/125454

gcc/cp/ChangeLog:

* typeck.cc (cp_build_array_ref): Don't recurse for array[array].

gcc/testsuite/ChangeLog:

* g++.dg/other/array8.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/other/array8.C [new file with mode: 0644]

index 483e664397bf873ac4b9201e1325ac69a0c20ea1..37a0c3cfb7ab0d1784148adb16505f7eadccbe1d 100644 (file)
@@ -4162,7 +4162,8 @@ cp_build_array_ref (location_t loc, tree array, tree idx,
     return error_mark_node;
 
   /* 0[array] */
-  if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
+  if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE
+      && TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
     {
       std::swap (array, idx);
 
diff --git a/gcc/testsuite/g++.dg/other/array8.C b/gcc/testsuite/g++.dg/other/array8.C
new file mode 100644 (file)
index 0000000..d238e3b
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/125454
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+void
+foo()
+{
+  T a = a[a]; // { dg-error "array subscript is not an integer" }
+}
+
+void
+bar ()
+{
+  foo<int[]>;
+}