diff --git a/flang/runtime/edit-input.cpp b/flang/runtime/edit-input.cpp --- a/flang/runtime/edit-input.cpp +++ b/flang/runtime/edit-input.cpp @@ -99,8 +99,8 @@ std::optional remaining; std::optional next; bool negate{ScanNumericPrefix(io, edit, next, remaining)}; - common::UnsignedInt128 value; - bool any{false}; + common::UnsignedInt128 value{0}; + bool any{negate}; for (; next; next = io.NextInField(remaining)) { char32_t ch{*next}; if (ch == ' ' || ch == '\t') { @@ -122,11 +122,11 @@ value += digit; any = true; } - if (any) { - if (negate) { - value = -value; - } - std::memcpy(n, &value, kind); + if (negate) { + value = -value; + } + if (any || !io.GetConnectionState().IsAtEOF()) { + std::memcpy(n, &value, kind); // a blank field means zero } return any; } @@ -155,7 +155,9 @@ } if (next.value_or(' ') == ' ') { // empty/blank field means zero remaining.reset(); - Put('0'); + if (!io.GetConnectionState().IsAtEOF()) { + Put('0'); + } return got; } char32_t decimal{GetDecimalPoint(edit)}; @@ -524,7 +526,7 @@ io.HandleRelativePosition(1); return EditDelimitedCharacterInput(io, x, length, *ch); } - if (IsNamelistName(io)) { + if (IsNamelistName(io) || io.GetConnectionState().IsAtEOF()) { return false; } // Undelimited list-directed character input: stop at a value separator @@ -563,6 +565,9 @@ edit.descriptor); return false; } + if (io.GetConnectionState().IsAtEOF()) { + return false; + } std::optional remaining{length}; if (edit.width && *edit.width > 0) { remaining = *edit.width; diff --git a/flang/runtime/internal-unit.cpp b/flang/runtime/internal-unit.cpp --- a/flang/runtime/internal-unit.cpp +++ b/flang/runtime/internal-unit.cpp @@ -39,7 +39,9 @@ template void InternalDescriptorUnit::EndIoStatement() { if constexpr (DIR == Direction::Output) { - if (furthestPositionInRecord > 0) { + // Clear the remainder of the current record if anything was written + // to it, or if it is the only record. + if (endfileRecordNumber.value_or(-1) == 2 || furthestPositionInRecord > 0) { BlankFillOutputRecord(); } }