Skip to content

[flang][runtime] Fix integer overflow check for FORMATs #79471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024

Conversation

klausler
Copy link
Contributor

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.

@klausler klausler requested a review from vzakhari January 25, 2024 17:02
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 25, 2024

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/79471.diff

1 Files Affected:

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

if (result >
std::numeric_limits<int>::max() / 10 - (static_cast<int>(ch) - '0')) {
constexpr int tenth{std::numeric_limits<int>::max() / 10};
if (result > tenth || ch - '0' > std::numeric_limits<int>::max() - result) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but shouldn't it look like || ch - '0' > std::numeric_limits<int>::max() - result * 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, sorry; will fix.

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 llvm#79255.
Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] Execution error of a large number as a repeat specification
3 participants