[flang-commits] [PATCH] D99296: [flang] Fix runtime crash on TRIM('')

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Mar 25 11:10:19 PDT 2021


klausler updated this revision to Diff 333361.
klausler added a comment.

One more tweak for clarity.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99296/new/

https://reviews.llvm.org/D99296

Files:
  flang/runtime/descriptor.cpp


Index: flang/runtime/descriptor.cpp
===================================================================
--- flang/runtime/descriptor.cpp
+++ flang/runtime/descriptor.cpp
@@ -31,9 +31,23 @@
     int rank, const SubscriptValue *extent, ISO::CFI_attribute_t attribute,
     bool addendum) {
   Terminator terminator{__FILE__, __LINE__};
+  // Subtle: the standard CFI_establish() function doesn't allow a zero
+  // elem_len argument in cases where elem_len is not ignored; and when it
+  // returns an error code (CFI_INVALID_ELEM_LEN in this case), it must not
+  // modify the descriptor.  That design makes sense, maybe, for actual
+  // C interoperability, but we need to work around it here.  A zero
+  // incoming element length is replaced by 4 so that it will be valid
+  // for all CHARACTER kinds.
+  std::size_t workaroundElemLen{elementBytes ? elementBytes : 4};
   RUNTIME_CHECK(terminator,
-      ISO::CFI_establish(&raw_, p, attribute, t.raw(), elementBytes, rank,
+      ISO::CFI_establish(&raw_, p, attribute, t.raw(), workaroundElemLen, rank,
           extent) == CFI_SUCCESS);
+  if (elementBytes == 0) {
+    raw_.elem_len = 0;
+    for (int j{0}; j < rank; ++j) {
+      GetDimension(j).SetByteStride(0);
+    }
+  }
   raw_.f18Addendum = addendum;
   DescriptorAddendum *a{Addendum()};
   RUNTIME_CHECK(terminator, addendum == (a != nullptr));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99296.333361.patch
Type: text/x-patch
Size: 1365 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210325/28f2ac5e/attachment-0001.bin>


More information about the flang-commits mailing list