Skip to content

Commit

Permalink
[flang][runtime] Catch infinite unlimited format repetition better
Browse files Browse the repository at this point in the history
The runtime check for infinite unlimited format repetition is missing
the case of implicit unlimited format repetition at the top level
without finding the next data edit descriptor that corresponds with
a data list item.

Replace the check with a Boolean flag that detects unlimited
repetition hitting a right parenthesis and restarting, and fail
when it happens the second time.

Differential Revision: https://reviews.llvm.org/D145745
  • Loading branch information
klausler committed Mar 10, 2023
1 parent c32c668 commit a1db3e6
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions flang/runtime/format-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static void HandleControl(CONTEXT &context, char ch, char next, int n) {
// format validator gauntlet.
template <typename CONTEXT>
int FormatControl<CONTEXT>::CueUpNextDataEdit(Context &context, bool stop) {
int unlimitedLoopCheck{-1};
bool hitUnlimitedLoopEnd{false};
// Do repetitions remain on an unparenthesized data edit?
while (height_ > 1 && format_[stack_[height_ - 1].start] != '(') {
offset_ = stack_[height_ - 1].start;
Expand Down Expand Up @@ -267,7 +267,6 @@ int FormatControl<CONTEXT>::CueUpNextDataEdit(Context &context, bool stop) {
RUNTIME_CHECK(context, format_[stack_[height_].start] == '(');
if (unlimited || height_ == 0) {
stack_[height_].remaining = Iteration::unlimited;
unlimitedLoopCheck = offset_ - 1;
} else if (repeat) {
if (*repeat <= 0) {
*repeat = 1; // error recovery
Expand Down Expand Up @@ -305,12 +304,13 @@ int FormatControl<CONTEXT>::CueUpNextDataEdit(Context &context, bool stop) {
restart);
return 0;
}
if (offset_ == unlimitedLoopCheck) {
if (hitUnlimitedLoopEnd) {
ReportBadFormat(context,
"Unlimited repetition in FORMAT lacks data edit descriptors",
restart);
return 0;
}
hitUnlimitedLoopEnd = true;
offset_ = restart;
} else if (stack_[height_ - 1].remaining-- > 0) {
offset_ = restart;
Expand Down

0 comments on commit a1db3e6

Please sign in to comment.