diff --git a/flang/runtime/format-implementation.h b/flang/runtime/format-implementation.h index c54ac062c7bea..9c342db2e19a2 100644 --- a/flang/runtime/format-implementation.h +++ b/flang/runtime/format-implementation.h @@ -85,8 +85,9 @@ int FormatControl::GetIntField( ch = PeekNext(); } while (ch >= '0' && ch <= '9') { - if (result > - std::numeric_limits::max() / 10 - (static_cast(ch) - '0')) { + constexpr int tenth{std::numeric_limits::max() / 10}; + if (result > tenth || + ch - '0' > std::numeric_limits::max() - 10 * result) { handler.SignalError( IostatErrorInFormat, "FORMAT integer field out of range"); if (hadError) {