[flang-commits] [flang] c7706d9 - [flang] Don't evaluate initializers for arrays with invalid rank (#171163)

via flang-commits flang-commits at lists.llvm.org
Wed Dec 17 10:58:56 PST 2025


Author: Leandro Lupori
Date: 2025-12-17T15:58:52-03:00
New Revision: c7706d9472fe880ba1d3418919ad4185710c9559

URL: https://github.com/llvm/llvm-project/commit/c7706d9472fe880ba1d3418919ad4185710c9559
DIFF: https://github.com/llvm/llvm-project/commit/c7706d9472fe880ba1d3418919ad4185710c9559.diff

LOG: [flang] Don't evaluate initializers for arrays with invalid rank (#171163)

Evaluating initializers for arrays with a rank greater than the
maximum supported can make the compiler run out of memory.

Fixes #124488

Added: 
    

Modified: 
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Semantics/maxrank.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 345a0e4e8ecce..eeb6df0def2f9 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1086,6 +1086,7 @@ class DeclarationVisitor : public ArraySpecVisitor,
       const parser::Name &, const parser::InitialDataTarget &);
   void PointerInitialization(
       const parser::Name &, const parser::ProcPointerInit &);
+  bool CheckRank(const Symbol &symbol);
   bool CheckNonPointerInitialization(
       const parser::Name &, bool inLegacyDataInitialization);
   void NonPointerInitialization(
@@ -9052,6 +9053,12 @@ void DeclarationVisitor::Initialization(const parser::Name &name,
     return;
   }
   Symbol &ultimate{name.symbol->GetUltimate()};
+  // Don't evaluate initializers for arrays with a rank greater than the
+  // maximum supported, to avoid running out of memory.
+  if (!CheckRank(ultimate)) {
+    return;
+  }
+
   // TODO: check C762 - all bounds and type parameters of component
   // are colons or constant expressions if component is initialized
   common::visit(
@@ -9178,6 +9185,18 @@ void DeclarationVisitor::PointerInitialization(
   }
 }
 
+bool DeclarationVisitor::CheckRank(const Symbol &symbol) {
+  if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
+    if (details->shape().Rank() > common::maxRank) {
+      Say(symbol.name(),
+          "'%s' has rank %d, which is greater than the maximum supported rank %d"_err_en_US,
+          symbol.name(), details->shape().Rank(), common::maxRank);
+      return false;
+    }
+  }
+  return true;
+}
+
 bool DeclarationVisitor::CheckNonPointerInitialization(
     const parser::Name &name, bool inLegacyDataInitialization) {
   if (!context().HasError(name.symbol)) {
@@ -10622,11 +10641,13 @@ class DeferredCheckVisitor {
 private:
   void Init(const parser::Name &name,
       const std::optional<parser::Initialization> &init) {
-    if (init) {
+    // Don't evaluate initializers for arrays with a rank greater than the
+    // maximum supported, to avoid running out of memory.
+    if (init && name.symbol && resolver_.CheckRank(*name.symbol)) {
       if (const auto *target{
               std::get_if<parser::InitialDataTarget>(&init->u)}) {
         resolver_.PointerInitialization(name, *target);
-      } else if (name.symbol) {
+      } else {
         if (const auto *object{name.symbol->detailsIf<ObjectEntityDetails>()};
             !object || !object->init()) {
           if (const auto *expr{std::get_if<parser::ConstantExpr>(&init->u)}) {

diff  --git a/flang/test/Semantics/maxrank.f90 b/flang/test/Semantics/maxrank.f90
index ea3ece95c670b..c3c57332789ed 100644
--- a/flang/test/Semantics/maxrank.f90
+++ b/flang/test/Semantics/maxrank.f90
@@ -17,6 +17,8 @@ module m
   real :: c
   dimension :: c(1,1,1,1,1,1,1,1,1,1,1,1,1,1)
   codimension :: c[1,*]
+  !ERROR: 'p' has rank 16, which is greater than the maximum supported rank 15
+  integer, parameter :: p(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) = 1
   interface
     !ERROR: 'foo' has rank 16, which is greater than the maximum supported rank 15
     real function foo()


        


More information about the flang-commits mailing list