Skip to content

Commit 32334b9

Browse files
authored
[flang][runtime] Fix integer overflow check for FORMATs (#79471)
The code that parses repeat counts, field widths, &c. from FORMAT strings has an incorrect overflow check, so the maximum integer value is not accepted. Fix. Fixes #79255.
1 parent 2b7a928 commit 32334b9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

flang/runtime/format-implementation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ int FormatControl<CONTEXT>::GetIntField(
8585
ch = PeekNext();
8686
}
8787
while (ch >= '0' && ch <= '9') {
88-
if (result >
89-
std::numeric_limits<int>::max() / 10 - (static_cast<int>(ch) - '0')) {
88+
constexpr int tenth{std::numeric_limits<int>::max() / 10};
89+
if (result > tenth ||
90+
ch - '0' > std::numeric_limits<int>::max() - 10 * result) {
9091
handler.SignalError(
9192
IostatErrorInFormat, "FORMAT integer field out of range");
9293
if (hadError) {

0 commit comments

Comments
 (0)