diff --git a/flang-rt/lib/runtime/edit-input.cpp b/flang-rt/lib/runtime/edit-input.cpp index 13557678f6057..50cbce1fd9049 100644 --- a/flang-rt/lib/runtime/edit-input.cpp +++ b/flang-rt/lib/runtime/edit-input.cpp @@ -375,21 +375,37 @@ static RT_API_ATTRS ScannedRealInput ScanRealInput( Put(*next); } } - if (next && *next == '(') { // NaN(...) - Put('('); - int depth{1}; - while (true) { - next = io.NextInField(remaining, edit); - if (depth == 0) { - break; - } else if (!next) { - return {}; // error - } else if (*next == '(') { - ++depth; - } else if (*next == ')') { - --depth; + if (first == 'N' && (!next || *next == '(') && + remaining.value_or(1) > 0) { // NaN(...)? + std::size_t byteCount{0}; + if (!next) { // NextInField won't return '(' for list-directed + next = io.GetCurrentChar(byteCount); + } + if (next && *next == '(') { + int depth{1}; + while (true) { + if (*next >= 'a' && *next <= 'z') { + *next = *next - 'a' + 'A'; + } + Put(*next); + io.HandleRelativePosition(byteCount); + io.GotChar(byteCount); + if (remaining) { + *remaining -= byteCount; + } + if (depth == 0) { + break; // done + } + next = io.GetCurrentChar(byteCount); + if (!next || remaining.value_or(1) < 1) { + return {}; // error + } else if (*next == '(') { + ++depth; + } else if (*next == ')') { + --depth; + } } - Put(*next); + next = io.NextInField(remaining, edit); } } } else if (first == radixPointChar || (first >= '0' && first <= '9') || diff --git a/flang-rt/unittests/Runtime/NumericalFormatTest.cpp b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp index 73245dca13bc0..c69a799f7f242 100644 --- a/flang-rt/unittests/Runtime/NumericalFormatTest.cpp +++ b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp @@ -964,6 +964,7 @@ TEST(IOApiTests, EditDoubleInputValues) { {"(RU,E9.1)", " 1.0E-325", 0x1, 0}, {"(E9.1)", "-1.0E-325", 0x8000000000000000, 0}, {"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0}, + {"(F7.0))", "+NaN(q)", 0x7ff8000000000000, 0}, }; for (auto const &[format, data, want, iostat] : testCases) { auto cookie{IONAME(BeginInternalFormattedInput)(