diff --git a/flang/runtime/descriptor-io.h b/flang/runtime/descriptor-io.h --- a/flang/runtime/descriptor-io.h +++ b/flang/runtime/descriptor-io.h @@ -223,16 +223,16 @@ 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 --- a/flang/runtime/descriptor.h +++ b/flang/runtime/descriptor.h @@ -255,6 +255,7 @@ } } + // 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)};