[flang-commits] [flang] cf63261 - [flang] Don't crash attempting to fold absurdly large TRANSFER()
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Fri Dec 16 11:43:28 PST 2022
Author: Peter Klausler
Date: 2022-12-16T11:43:14-08:00
New Revision: cf63261b7789ba42ca6db04cfec54a8524d7487b
URL: https://github.com/llvm/llvm-project/commit/cf63261b7789ba42ca6db04cfec54a8524d7487b
DIFF: https://github.com/llvm/llvm-project/commit/cf63261b7789ba42ca6db04cfec54a8524d7487b.diff
LOG: [flang] Don't crash attempting to fold absurdly large TRANSFER()
Apply some sanity checking to the type size and SIZE= values and
avoid crashing on stress tests that attempt to create excessively
large results.
Differential Revision: https://reviews.llvm.org/D140138
Added:
Modified:
flang/lib/Evaluate/fold.cpp
Removed:
################################################################################
diff --git a/flang/lib/Evaluate/fold.cpp b/flang/lib/Evaluate/fold.cpp
index c78a0aa4c9ad..6f61a6829484 100644
--- a/flang/lib/Evaluate/fold.cpp
+++ b/flang/lib/Evaluate/fold.cpp
@@ -261,14 +261,21 @@ std::optional<Expr<SomeType>> FoldTransfer(
}
}
if (sourceBytes && IsActuallyConstant(*source) && moldType && extents) {
- InitialImage image{*sourceBytes};
- InitialImage::Result imageResult{
- image.Add(0, *sourceBytes, *source, context)};
- CHECK(imageResult == InitialImage::Ok);
- return image.AsConstant(context, *moldType, *extents, true /*pad with 0*/);
- } else {
- return std::nullopt;
+ std::size_t elements{
+ extents->empty() ? 1 : static_cast<std::size_t>((*extents)[0])};
+ std::size_t totalBytes{*sourceBytes * elements};
+ // Don't fold intentional overflow cases from sneaky tests
+ if (totalBytes < std::size_t{1000000} &&
+ (elements == 0 || totalBytes / elements == *sourceBytes)) {
+ InitialImage image{*sourceBytes};
+ InitialImage::Result imageResult{
+ image.Add(0, *sourceBytes, *source, context)};
+ CHECK(imageResult == InitialImage::Ok);
+ return image.AsConstant(
+ context, *moldType, *extents, true /*pad with 0*/);
+ }
}
+ return std::nullopt;
}
template class ExpressionBase<SomeDerived>;
More information about the flang-commits
mailing list