[flang-commits] [PATCH] D126155: flang][runtime] Catch decimal integer input overflow
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Sat May 21 22:20:50 PDT 2022
klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.
[B/O/Z input overflow is already caught, and real input overflow
is signalled as an IEEE arithmetic exception, but regular decimal
integer overflow was silent.
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
@@ -83,6 +83,15 @@
return "Bad REAL input value";
case IostatBadScaleFactor:
return "Bad REAL output scale factor (kP)";
+ case IostatBadAsynchronous:
+ return "READ/WRITE(ASYNCHRONOUS='YES') on unit without "
+ "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
@@ -70,6 +70,10 @@
IostatUnitOverflow,
IostatBadRealInput,
IostatBadScaleFactor,
+ IostatBadAsynchronous,
+ IostatBadWaitUnit,
+ IostatBOZInputOverflow,
+ IostatIntegerInputOverflow,
};
const char *IostatErrorString(int);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126155.431212.patch
Type: text/x-patch
Size: 2806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220522/c3c0ce58/attachment-0001.bin>
More information about the flang-commits
mailing list