[flang-commits] [PATCH] D142770: [flang] Don't fold REPEAT() when the result would be too large

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 09:56:58 PST 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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


https://reviews.llvm.org/D142770

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


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -100,6 +100,10 @@
     !CHECK: error: DIM=4 argument to SPREAD must be between 1 and 3
     integer, parameter :: bad3 = spread(matrix, 4, 1)
   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
Index: flang/lib/Evaluate/fold-character.cpp
===================================================================
--- flang/lib/Evaluate/fold-character.cpp
+++ flang/lib/Evaluate/fold-character.cpp
@@ -91,9 +91,16 @@
   } 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{
Index: flang/lib/Evaluate/character.h
===================================================================
--- flang/lib/Evaluate/character.h
+++ flang/lib/Evaluate/character.h
@@ -106,6 +106,7 @@
   static Character REPEAT(const Character &str, ConstantSubscript ncopies) {
     Character result;
     if (!str.empty()) {
+      result.reserve(ncopies * str.size());
       while (ncopies-- > 0) {
         result += str;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142770.492830.patch
Type: text/x-patch
Size: 2255 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230127/b1a50ab3/attachment-0001.bin>


More information about the flang-commits mailing list