From cd117fa04b620c9ce4d63ca4d475aa3ec5a1a122 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Thu, 18 Aug 2022 10:31:13 -0700 Subject: [PATCH] [flang][runtime] Fix return value for MINVAL/MAXVAL for CHARACTER(kind > 1) CharacterExtremumAccumulator::GetResult() needs to use byte counts, not wide character counts, when calling memcpy() & memset(). Differential Revision: https://reviews.llvm.org/D132156 --- flang/runtime/extrema.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/flang/runtime/extrema.cpp b/flang/runtime/extrema.cpp index 709a032..8290ad3 100644 --- a/flang/runtime/extrema.cpp +++ b/flang/runtime/extrema.cpp @@ -419,11 +419,16 @@ public: void Reinitialize() { extremum_ = nullptr; } template void GetResult(A *p, int /*zeroBasedDim*/ = -1) const { static_assert(std::is_same_v); + std::size_t byteSize{array_.ElementBytes()}; if (extremum_) { - std::memcpy(p, extremum_, charLen_); + std::memcpy(p, extremum_, byteSize); } else { - // empty array: result is all zero-valued characters - std::memset(p, 0, charLen_); + // empty array + if constexpr (KIND == 1) { // ASCII + *p = IS_MAXVAL ? 0 : 127; // 127 required by standard + } else { + std::memset(p, IS_MAXVAL ? 0 : 255, byteSize); + } } } bool Accumulate(const Type *x) { -- 2.7.4