[flang-commits] [PATCH] D126155: flang][runtime] Catch decimal integer input overflow
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Tue May 24 14:15:30 PDT 2022
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcdd54cbdd937: [flang][runtime] Catch decimal integer input overflow (authored by klausler).
Changed prior to commit:
https://reviews.llvm.org/D126155?vs=431212&id=431792#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D126155/new/
https://reviews.llvm.org/D126155
Files:
flang/include/flang/Runtime/iostat.h
flang/runtime/edit-input.cpp
flang/runtime/iostat.cpp
Index: flang/runtime/iostat.cpp
===================================================================
--- flang/runtime/iostat.cpp
+++ flang/runtime/iostat.cpp
@@ -88,6 +88,10 @@
"OPEN(ASYNCHRONOUS='YES')";
case IostatBadWaitUnit:
return "WAIT(ID=nonzero) for a bad unit number";
+ case IostatBOZInputOverflow:
+ return "B/O/Z input value overflows variable";
+ case IostatIntegerInputOverflow:
+ return "Integer input value overflows variable";
default:
return nullptr;
}
Index: flang/runtime/edit-input.cpp
===================================================================
--- flang/runtime/edit-input.cpp
+++ flang/runtime/edit-input.cpp
@@ -47,7 +47,7 @@
}
auto significantBytes{static_cast<std::size_t>(digits * LOG2_BASE + 7) / 8};
if (significantBytes > bytes) {
- io.GetIoErrorHandler().SignalError(
+ io.GetIoErrorHandler().SignalError(IostatBOZInputOverflow,
"B/O/Z input of %d digits overflows %zd-byte variable", digits, bytes);
return false;
}
@@ -140,6 +140,7 @@
bool negate{ScanNumericPrefix(io, edit, next, remaining)};
common::UnsignedInt128 value{0};
bool any{negate};
+ bool overflow{false};
for (; next; next = io.NextInField(remaining, edit)) {
char32_t ch{*next};
if (ch == ' ' || ch == '\t') {
@@ -157,10 +158,23 @@
"Bad character '%lc' in INTEGER input field", ch);
return false;
}
+ static constexpr auto maxu128{~common::UnsignedInt128{0}};
+ static constexpr auto maxu128OverTen{maxu128 / 10};
+ static constexpr int maxLastDigit{
+ static_cast<int>(maxu128 - (maxu128OverTen * 10))};
+ overflow |= value >= maxu128OverTen &&
+ (value > maxu128OverTen || digit > maxLastDigit);
value *= 10;
value += digit;
any = true;
}
+ auto maxForKind{common::UnsignedInt128{1} << ((8 * kind) - 1)};
+ overflow |= value >= maxForKind && (value > maxForKind || !negate);
+ if (overflow) {
+ io.GetIoErrorHandler().SignalError(IostatIntegerInputOverflow,
+ "Decimal input overflows INTEGER(%d) variable", kind);
+ return false;
+ }
if (negate) {
value = -value;
}
Index: flang/include/flang/Runtime/iostat.h
===================================================================
--- flang/include/flang/Runtime/iostat.h
+++ flang/include/flang/Runtime/iostat.h
@@ -72,6 +72,8 @@
IostatBadScaleFactor,
IostatBadAsynchronous,
IostatBadWaitUnit,
+ IostatBOZInputOverflow,
+ IostatIntegerInputOverflow,
};
const char *IostatErrorString(int);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126155.431792.patch
Type: text/x-patch
Size: 2551 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220524/876685c6/attachment.bin>
More information about the flang-commits
mailing list