[flang-commits] [flang] 23f258c - [flang] Don't fold REPEAT() when the result would be too large

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sun Jan 29 12:14:51 PST 2023


Author: Peter Klausler
Date: 2023-01-29T12:14:41-08:00
New Revision: 23f258cebbeedd270154521ec3cef7e3b1035431

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

LOG: [flang] Don't fold REPEAT() when the result would be too large

A test program shouldn't be able to crash the compiler by getting it to
fold REPEAT() with an absurdly large repetition count.

Differential Revision: https://reviews.llvm.org/D142770

Added: 
    

Modified: 
    flang/lib/Evaluate/character.h
    flang/lib/Evaluate/fold-character.cpp
    flang/test/Evaluate/errors01.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/character.h b/flang/lib/Evaluate/character.h
index 794872605956c..9742e875b9e15 100644
--- a/flang/lib/Evaluate/character.h
+++ b/flang/lib/Evaluate/character.h
@@ -106,6 +106,7 @@ template <int KIND> class CharacterUtils {
   static Character REPEAT(const Character &str, ConstantSubscript ncopies) {
     Character result;
     if (!str.empty()) {
+      result.reserve(ncopies * str.size());
       while (ncopies-- > 0) {
         result += str;
       }

diff  --git a/flang/lib/Evaluate/fold-character.cpp b/flang/lib/Evaluate/fold-character.cpp
index 56214ea14ef35..3aec77b113cce 100644
--- a/flang/lib/Evaluate/fold-character.cpp
+++ b/flang/lib/Evaluate/fold-character.cpp
@@ -97,9 +97,16 @@ Expr<Type<TypeCategory::Character, KIND>> FoldIntrinsicFunction(
   } else if (name == "repeat") { // not elemental
     if (auto scalars{GetScalarConstantArguments<T, SubscriptInteger>(
             context, funcRef.arguments())}) {
-      return Expr<T>{Constant<T>{
-          CharacterUtils<KIND>::REPEAT(std::get<Scalar<T>>(*scalars),
-              std::get<Scalar<SubscriptInteger>>(*scalars).ToInt64())}};
+      auto str{std::get<Scalar<T>>(*scalars)};
+      auto n{std::get<Scalar<SubscriptInteger>>(*scalars).ToInt64()};
+      if (static_cast<double>(n) * str.size() >
+          (1 << 20)) { // sanity limit of 1MiB
+        context.messages().Say(
+            "Result of REPEAT() is too large to compute at compilation time (%g characters)"_port_en_US,
+            static_cast<double>(n) * str.size());
+      } else {
+        return Expr<T>{Constant<T>{CharacterUtils<KIND>::REPEAT(str, n)}};
+      }
     }
   } else if (name == "trim") { // not elemental
     if (auto scalar{

diff  --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index d2fcc95cfdab9..72ad988fc2042 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -137,6 +137,10 @@ subroutine s12(x,y)
     !CHERK: error: Must be a constant value
     integer, parameter :: bad2 = storage_size(y)
   end subroutine
+  subroutine s13
+    !CHECK: portability: Result of REPEAT() is too large to compute at compilation time (1.1259e+15 characters)
+    print *, repeat(repeat(' ', 2**20), 2**30)
+  end subroutine
   subroutine warnings
     real, parameter :: ok1 = scale(0.0, 99999) ! 0.0
     real, parameter :: ok2 = scale(1.0, -99999) ! 0.0


        


More information about the flang-commits mailing list