[flang] Fix an assert when RESHAPE() is called on empty strings
authorPete Steinfeld <psteinfeld@nvidia.com>
Wed, 22 Jul 2020 18:33:35 +0000 (11:33 -0700)
committerPete Steinfeld <psteinfeld@nvidia.com>
Wed, 22 Jul 2020 19:21:58 +0000 (12:21 -0700)
Summary:
When a constant array of empty strings goes through contant folding, the result
is something that contains no bytes.  If this array is passed to the intrinsic
function `RESHAPE()`, we were not handling things correctly.  I fixed this by
checking for an empty destination when calling the function `CopyFrom()` on an
array of strings.

I also added a test with a couple of different examples that trigger the
problem.

Reviewers: klausler, tskeith, DavidTruby

Subscribers: llvm-commits

Tags: #llvm

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

flang/lib/Evaluate/constant.cpp
flang/test/Semantics/modfile25.f90

index ecdcc0a..e749763 100644 (file)
@@ -244,19 +244,26 @@ std::size_t Constant<Type<TypeCategory::Character, KIND>>::CopyFrom(
     std::size_t count, ConstantSubscripts &resultSubscripts,
     const std::vector<int> *dimOrder) {
   CHECK(length_ == source.length_);
-  std::size_t copied{0};
-  std::size_t elementBytes{length_ * sizeof(decltype(values_[0]))};
-  ConstantSubscripts sourceSubscripts{source.lbounds()};
-  while (copied < count) {
-    auto *dest{&values_.at(SubscriptsToOffset(resultSubscripts) * length_)};
-    const auto *src{&source.values_.at(
-        source.SubscriptsToOffset(sourceSubscripts) * length_)};
-    std::memcpy(dest, src, elementBytes);
-    copied++;
-    source.IncrementSubscripts(sourceSubscripts);
-    IncrementSubscripts(resultSubscripts, dimOrder);
+  if (length_ == 0) {
+    // It's possible that the array of strings consists of all empty strings.
+    // If so, constant folding will result in a string that's completely empty
+    // and the length_ will be zero, and there's nothing to do.
+    return count;
+  } else {
+    std::size_t copied{0};
+    std::size_t elementBytes{length_ * sizeof(decltype(values_[0]))};
+    ConstantSubscripts sourceSubscripts{source.lbounds()};
+    while (copied < count) {
+      auto *dest{&values_.at(SubscriptsToOffset(resultSubscripts) * length_)};
+      const auto *src{&source.values_.at(
+          source.SubscriptsToOffset(sourceSubscripts) * length_)};
+      std::memcpy(dest, src, elementBytes);
+      copied++;
+      source.IncrementSubscripts(sourceSubscripts);
+      IncrementSubscripts(resultSubscripts, dimOrder);
+    }
+    return copied;
   }
-  return copied;
 }
 
 // Constant<SomeDerived> specialization
index 850298d..76dc61a 100644 (file)
@@ -22,6 +22,9 @@ module m1
     real, intent(in) :: x(:,:)
     integer, intent(in) :: n1(3), n2(:)
     real, allocatable :: a(:,:,:)
+    ! the following fail if we don't handle empty strings
+    Character(0) :: ch1(1,2,3) = Reshape([('',n=1,1*2*3)],[1,2,3])
+    Character(0) :: ch2(3) = reshape(['','',''], [3])
     a = reshape(x,n1)
     a = reshape(x,n2(10:30:9)) ! fails if we can't figure out triplet shape
   end subroutine