[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
Fri Dec 16 11:43:35 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGcf63261b7789: [flang] Don't crash attempting to fold absurdly large TRANSFER() (authored by klausler).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140138/new/

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.483622.patch
Type: text/x-patch
Size: 1277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221216/992d0443/attachment.bin>


More information about the flang-commits mailing list