Skip to content

Commit 514b759

Browse files
committed
[flang][runtime] Catch input error case of missing integer value
Formatted input allows for an empty numeric input field, which signifies a zero value, but list-directed and NAMELIST input does not -- apart from the special case of a NAMELIST array. Differential Revision: https://reviews.llvm.org/D132178
1 parent aaccb23 commit 514b759

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

flang/runtime/edit-input.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bool EditIntegerInput(
118118
RUNTIME_CHECK(io.GetIoErrorHandler(), kind >= 1 && !(kind & (kind - 1)));
119119
switch (edit.descriptor) {
120120
case DataEdit::ListDirected:
121-
if (IsNamelistName(io)) {
121+
if (IsNamelistNameOrSlash(io)) {
122122
return false;
123123
}
124124
break;
@@ -172,6 +172,11 @@ bool EditIntegerInput(
172172
value += digit;
173173
any = true;
174174
}
175+
if (!any && !remaining) {
176+
io.GetIoErrorHandler().SignalError(
177+
"Integer value absent from NAMELIST or list-directed input");
178+
return false;
179+
}
175180
auto maxForKind{common::UnsignedInt128{1} << ((8 * kind) - 1)};
176181
overflow |= value >= maxForKind && (value > maxForKind || !negate);
177182
if (overflow) {
@@ -527,7 +532,7 @@ template <int KIND>
527532
bool EditRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
528533
switch (edit.descriptor) {
529534
case DataEdit::ListDirected:
530-
if (IsNamelistName(io)) {
535+
if (IsNamelistNameOrSlash(io)) {
531536
return false;
532537
}
533538
return EditCommonRealInput<KIND>(io, edit, n);
@@ -561,7 +566,7 @@ bool EditRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
561566
bool EditLogicalInput(IoStatementState &io, const DataEdit &edit, bool &x) {
562567
switch (edit.descriptor) {
563568
case DataEdit::ListDirected:
564-
if (IsNamelistName(io)) {
569+
if (IsNamelistNameOrSlash(io)) {
565570
return false;
566571
}
567572
break;
@@ -650,7 +655,7 @@ static bool EditListDirectedCharacterInput(
650655
io.HandleRelativePosition(byteCount);
651656
return EditDelimitedCharacterInput(io, x, length, *ch);
652657
}
653-
if (IsNamelistName(io) || io.GetConnectionState().IsAtEOF()) {
658+
if (IsNamelistNameOrSlash(io) || io.GetConnectionState().IsAtEOF()) {
654659
return false;
655660
}
656661
// Undelimited list-directed character input: stop at a value separator

flang/runtime/namelist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
493493
return true;
494494
}
495495

496-
bool IsNamelistName(IoStatementState &io) {
496+
bool IsNamelistNameOrSlash(IoStatementState &io) {
497497
if (auto *listInput{
498498
io.get_if<ListDirectedStatementState<Direction::Input>>()}) {
499499
if (listInput->inNamelistArray()) {
@@ -508,6 +508,8 @@ bool IsNamelistName(IoStatementState &io) {
508508
ch = io.GetNextNonBlank(byteCount);
509509
// TODO: how to deal with NaN(...) ambiguity?
510510
return ch && (*ch == '=' || *ch == '(' || *ch == '%');
511+
} else {
512+
return *ch == '/';
511513
}
512514
}
513515
}

flang/runtime/namelist.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ class NamelistGroup {
3535
const Item *item; // in original declaration order
3636
};
3737

38-
// Look ahead on input for an identifier followed by a '=', '(', or '%'
38+
// Look ahead on input for a '/' or an identifier followed by a '=', '(', or '%'
3939
// character; for use in disambiguating a name-like value (e.g. F or T) from a
40-
// NAMELIST group item name. Always false when not reading a NAMELIST.
41-
bool IsNamelistName(IoStatementState &);
40+
// NAMELIST group item name and for coping with short arrays. Always false
41+
// when not reading a NAMELIST.
42+
bool IsNamelistNameOrSlash(IoStatementState &);
4243

4344
} // namespace Fortran::runtime::io
4445
#endif // FORTRAN_RUNTIME_NAMELIST_H_

0 commit comments

Comments
 (0)