[flang-commits] [flang] db6c3ec - [flang][runtime] Make NCOPIES= argument of REPEAT a signed integer, & check it

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jun 15 14:33:43 PDT 2022


Author: Peter Klausler
Date: 2022-06-15T14:29:17-07:00
New Revision: db6c3ecd2c7a296c1aa1120719d9659d08965448

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

LOG: [flang][runtime] Make NCOPIES= argument of REPEAT a signed integer, & check it

NCOPIES= is currently a std::size_t in the API.  If a negative value is
used, the memory allocation will fail.  Change it to be a signed integer,
and crash with a message instead if it be negative.

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

Added: 
    

Modified: 
    flang/include/flang/Runtime/character.h
    flang/runtime/character.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Runtime/character.h b/flang/include/flang/Runtime/character.h
index 441faf2c36333..24f26920bdd2c 100644
--- a/flang/include/flang/Runtime/character.h
+++ b/flang/include/flang/Runtime/character.h
@@ -13,6 +13,7 @@
 #define FORTRAN_RUNTIME_CHARACTER_H_
 #include "flang/Runtime/entry-names.h"
 #include <cstddef>
+#include <cstdint>
 
 namespace Fortran::runtime {
 
@@ -97,7 +98,7 @@ std::size_t RTNAME(LenTrim4)(const char32_t *, std::size_t);
 void RTNAME(LenTrim)(Descriptor &result, const Descriptor &, int kind,
     const char *sourceFile = nullptr, int sourceLine = 0);
 void RTNAME(Repeat)(Descriptor &result, const Descriptor &string,
-    std::size_t ncopies, const char *sourceFile = nullptr, int sourceLine = 0);
+    std::int64_t ncopies, const char *sourceFile = nullptr, int sourceLine = 0);
 void RTNAME(Trim)(Descriptor &result, const Descriptor &string,
     const char *sourceFile = nullptr, int sourceLine = 0);
 

diff  --git a/flang/runtime/character.cpp b/flang/runtime/character.cpp
index 2264fe6b8f62c..16f39332cc60c 100644
--- a/flang/runtime/character.cpp
+++ b/flang/runtime/character.cpp
@@ -953,8 +953,12 @@ void RTNAME(Scan)(Descriptor &result, const Descriptor &string,
 }
 
 void RTNAME(Repeat)(Descriptor &result, const Descriptor &string,
-    std::size_t ncopies, const char *sourceFile, int sourceLine) {
+    std::int64_t ncopies, const char *sourceFile, int sourceLine) {
   Terminator terminator{sourceFile, sourceLine};
+  if (ncopies < 0) {
+    terminator.Crash(
+        "REPEAT has negative NCOPIES=%jd", static_cast<std::intmax_t>(ncopies));
+  }
   std::size_t origBytes{string.ElementBytes()};
   result.Establish(string.type(), origBytes * ncopies, nullptr, 0, nullptr,
       CFI_attribute_allocatable);


        


More information about the flang-commits mailing list