From 5c9aca1e9396c54151b066189748209554230b59 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Wed, 12 Aug 2020 16:25:26 -0700 Subject: [PATCH] [flang] Descriptor-based I/O using wrong size for contiguous unformatted I/O The descriptor-based I/O routine was using the size of the descriptor rather than the size of the described data for the transfer. Fix, and add a comment to the relevant API. Differential Revision: https://reviews.llvm.org/D85863 --- flang/runtime/descriptor-io.h | 4 ++-- flang/runtime/descriptor.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/flang/runtime/descriptor-io.h b/flang/runtime/descriptor-io.h index 22ae4f9917ab..ce0f39740c5f 100644 --- a/flang/runtime/descriptor-io.h +++ b/flang/runtime/descriptor-io.h @@ -223,16 +223,16 @@ static bool DescriptorIO(IoStatementState &io, const Descriptor &descriptor) { std::size_t elementBytes{descriptor.ElementBytes()}; SubscriptValue subscripts[maxRank]; descriptor.GetLowerBounds(subscripts); + std::size_t numElements{descriptor.Elements()}; if (descriptor.IsContiguous()) { // contiguous unformatted I/O char &x{ExtractElement(io, descriptor, subscripts)}; - auto totalBytes{descriptor.SizeInBytes()}; + auto totalBytes{numElements * elementBytes}; if constexpr (DIR == Direction::Output) { return unf->Emit(&x, totalBytes, elementBytes); } else { return unf->Receive(&x, totalBytes, elementBytes); } } else { // non-contiguous unformatted I/O - std::size_t numElements{descriptor.Elements()}; for (std::size_t j{0}; j < numElements; ++j) { char &x{ExtractElement(io, descriptor, subscripts)}; if constexpr (DIR == Direction::Output) { diff --git a/flang/runtime/descriptor.h b/flang/runtime/descriptor.h index 3462bb612dff..983d483788b9 100644 --- a/flang/runtime/descriptor.h +++ b/flang/runtime/descriptor.h @@ -255,6 +255,7 @@ public: } } + // Returns size in bytes of the descriptor (not the data) static constexpr std::size_t SizeInBytes( int rank, bool addendum = false, int lengthTypeParameters = 0) { std::size_t bytes{sizeof(Descriptor) - sizeof(Dimension)};