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

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Jun 14 14:04:27 PDT 2022


klausler created this revision.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D127795

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


Index: flang/runtime/character.cpp
===================================================================
--- flang/runtime/character.cpp
+++ flang/runtime/character.cpp
@@ -953,8 +953,12 @@
 }
 
 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);
Index: flang/include/flang/Runtime/character.h
===================================================================
--- flang/include/flang/Runtime/character.h
+++ 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 @@
 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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127795.436930.patch
Type: text/x-patch
Size: 1620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220614/0cf1129c/attachment.bin>


More information about the flang-commits mailing list