[flang-commits] [flang] [flang] Fix crash on erroneous program (PR #88192)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Apr 9 13:48:11 PDT 2024


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/88192

Constant folding had a CHECK on array subscript rank that should more gracefully handle a bad program with a subscript that is a matrix or higher rank.

Fixes https://github.com/llvm/llvm-project/issues/88112.

>From 2cc9608a4b4aa4a20bae1c439b2d39a8e3172b10 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 9 Apr 2024 13:44:54 -0700
Subject: [PATCH] [flang] Fix crash on erroneous program

Constant folding had a CHECK on array subscript rank that should
more gracefully handle a bad program with a subscript that is
a matrix or higher rank.

Fixes https://github.com/llvm/llvm-project/issues/88112.
---
 flang/lib/Evaluate/fold-implementation.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h
index 470dbe9e740997..543d415209271b 100644
--- a/flang/lib/Evaluate/fold-implementation.h
+++ b/flang/lib/Evaluate/fold-implementation.h
@@ -201,11 +201,12 @@ std::optional<Constant<T>> Folder<T>::ApplySubscripts(const Constant<T> &array,
   ConstantSubscripts resultShape;
   ConstantSubscripts ssLB;
   for (const auto &ss : subscripts) {
-    CHECK(ss.Rank() <= 1);
     if (ss.Rank() == 1) {
       resultShape.push_back(static_cast<ConstantSubscript>(ss.size()));
       elements *= ss.size();
       ssLB.push_back(ss.lbounds().front());
+    } else if (ss.Rank() > 1) {
+      return std::nullopt; // error recovery
     }
   }
   ConstantSubscripts ssAt(rank, 0), at(rank, 0), tmp(1, 0);



More information about the flang-commits mailing list