[flang-commits] [flang] 8151d6f - [flang] Diagnose REPEAT with negative NCOPIES=

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Feb 13 14:49:00 PST 2023


Author: Peter Klausler
Date: 2023-02-13T14:48:47-08:00
New Revision: 8151d6f804586f35a55300651bcd1d10581b131a

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

LOG: [flang] Diagnose REPEAT with negative NCOPIES=

Emit an error when the NCOPIES= argument to the intrinsic function
REPEAT has a negative value.  (The current implementation crashes, which
isn't informative.)

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

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 9742e875b9e15..ca24dd8e9413d 100644
--- a/flang/lib/Evaluate/character.h
+++ b/flang/lib/Evaluate/character.h
@@ -105,7 +105,7 @@ template <int KIND> class CharacterUtils {
 
   static Character REPEAT(const Character &str, ConstantSubscript ncopies) {
     Character result;
-    if (!str.empty()) {
+    if (!str.empty() && ncopies > 0) {
       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 3aec77b113cce..5fb2271f0b2e9 100644
--- a/flang/lib/Evaluate/fold-character.cpp
+++ b/flang/lib/Evaluate/fold-character.cpp
@@ -99,7 +99,11 @@ Expr<Type<TypeCategory::Character, KIND>> FoldIntrinsicFunction(
             context, funcRef.arguments())}) {
       auto str{std::get<Scalar<T>>(*scalars)};
       auto n{std::get<Scalar<SubscriptInteger>>(*scalars).ToInt64()};
-      if (static_cast<double>(n) * str.size() >
+      if (n < 0) {
+        context.messages().Say(
+            "NCOPIES= argument to REPEAT() should be nonnegative, but is %jd"_err_en_US,
+            static_cast<std::intmax_t>(n));
+      } else 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,

diff  --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index 46cc4e6717588..0b86ccb199ccd 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -148,6 +148,8 @@ subroutine s12(x,y)
   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)
+    !CHECK: error: NCOPIES= argument to REPEAT() should be nonnegative, but is -666
+    print *, repeat(' ', -666)
   end subroutine
   subroutine warnings
     real, parameter :: ok1 = scale(0.0, 99999) ! 0.0


        


More information about the flang-commits mailing list