[flang-commits] [PATCH] D140138: [flang] Don't crash attempting to fold absurdly large TRANSFER()

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Dec 15 11:29:16 PST 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.

Apply some sanity checking to the type size and SIZE= values and 
avoid crashing on stress tests that attempt to create excessively
large results.


https://reviews.llvm.org/D140138

Files:
  flang/lib/Evaluate/fold.cpp


Index: flang/lib/Evaluate/fold.cpp
===================================================================
--- flang/lib/Evaluate/fold.cpp
+++ flang/lib/Evaluate/fold.cpp
@@ -261,14 +261,21 @@
     }
   }
   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>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140138.483261.patch
Type: text/x-patch
Size: 1277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221215/63a60595/attachment-0001.bin>


More information about the flang-commits mailing list