Skip to content

Commit f6a6cdd

Browse files
authored
[flang][runtime] Fix formatted input of NAN(...) (#149606)
Formatted real input is allowed to have parenthesized information after "NAN". We don't interpret the contents, but we should at least scan the information correctly. Fixes #149533 and #150035.
1 parent 1d81dfa commit f6a6cdd

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

flang-rt/lib/runtime/edit-input.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,37 @@ static RT_API_ATTRS ScannedRealInput ScanRealInput(
375375
Put(*next);
376376
}
377377
}
378-
if (next && *next == '(') { // NaN(...)
379-
Put('(');
380-
int depth{1};
381-
while (true) {
382-
next = io.NextInField(remaining, edit);
383-
if (depth == 0) {
384-
break;
385-
} else if (!next) {
386-
return {}; // error
387-
} else if (*next == '(') {
388-
++depth;
389-
} else if (*next == ')') {
390-
--depth;
378+
if (first == 'N' && (!next || *next == '(') &&
379+
remaining.value_or(1) > 0) { // NaN(...)?
380+
std::size_t byteCount{0};
381+
if (!next) { // NextInField won't return '(' for list-directed
382+
next = io.GetCurrentChar(byteCount);
383+
}
384+
if (next && *next == '(') {
385+
int depth{1};
386+
while (true) {
387+
if (*next >= 'a' && *next <= 'z') {
388+
*next = *next - 'a' + 'A';
389+
}
390+
Put(*next);
391+
io.HandleRelativePosition(byteCount);
392+
io.GotChar(byteCount);
393+
if (remaining) {
394+
*remaining -= byteCount;
395+
}
396+
if (depth == 0) {
397+
break; // done
398+
}
399+
next = io.GetCurrentChar(byteCount);
400+
if (!next || remaining.value_or(1) < 1) {
401+
return {}; // error
402+
} else if (*next == '(') {
403+
++depth;
404+
} else if (*next == ')') {
405+
--depth;
406+
}
391407
}
392-
Put(*next);
408+
next = io.NextInField(remaining, edit);
393409
}
394410
}
395411
} else if (first == radixPointChar || (first >= '0' && first <= '9') ||

flang-rt/unittests/Runtime/NumericalFormatTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ TEST(IOApiTests, EditDoubleInputValues) {
964964
{"(RU,E9.1)", " 1.0E-325", 0x1, 0},
965965
{"(E9.1)", "-1.0E-325", 0x8000000000000000, 0},
966966
{"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0},
967+
{"(F7.0))", "+NaN(q)", 0x7ff8000000000000, 0},
967968
};
968969
for (auto const &[format, data, want, iostat] : testCases) {
969970
auto cookie{IONAME(BeginInternalFormattedInput)(

0 commit comments

Comments
 (0)