diff --git a/flang/lib/Evaluate/constant.cpp b/flang/lib/Evaluate/constant.cpp --- a/flang/lib/Evaluate/constant.cpp +++ b/flang/lib/Evaluate/constant.cpp @@ -244,19 +244,26 @@ std::size_t count, ConstantSubscripts &resultSubscripts, const std::vector *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 specialization diff --git a/flang/test/Semantics/modfile25.f90 b/flang/test/Semantics/modfile25.f90 --- a/flang/test/Semantics/modfile25.f90 +++ b/flang/test/Semantics/modfile25.f90 @@ -22,6 +22,9 @@ 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