[flang-commits] [flang] d484fe9 - [flang] Don't crash on initialization with a zero-sized derived type

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Jun 4 09:03:17 PDT 2022


Author: Peter Klausler
Date: 2022-06-04T08:58:16-07:00
New Revision: d484fe93d4e827b4986b15f9db52099c925139b0

URL: https://github.com/llvm/llvm-project/commit/d484fe93d4e827b4986b15f9db52099c925139b0
DIFF: https://github.com/llvm/llvm-project/commit/d484fe93d4e827b4986b15f9db52099c925139b0.diff

LOG: [flang] Don't crash on initialization with a zero-sized derived type

Avoid calls to memcpy with zero byte counts if their address argument
calculations may not be valid expressions.

Differential Revision: https://reviews.llvm.org/D127027

Added: 
    

Modified: 
    flang/include/flang/Evaluate/initial-image.h
    flang/lib/Evaluate/initial-image.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/initial-image.h b/flang/include/flang/Evaluate/initial-image.h
index fcc18835418a8..90a781ba091fc 100644
--- a/flang/include/flang/Evaluate/initial-image.h
+++ b/flang/include/flang/Evaluate/initial-image.h
@@ -49,6 +49,8 @@ class InitialImage {
           bytes !=
               x.values().size() * static_cast<std::size_t>(*elementBytes)) {
         return SizeMismatch;
+      } else if (bytes == 0) {
+        return Ok;
       } else {
         std::memcpy(&data_.at(offset), &x.values().at(0), bytes);
         return Ok;
@@ -66,6 +68,8 @@ class InitialImage {
       auto elementBytes{bytes > 0 ? bytes / elements : 0};
       if (elements * elementBytes != bytes) {
         return SizeMismatch;
+      } else if (bytes == 0) {
+        return Ok;
       } else {
         for (auto at{x.lbounds()}; elements-- > 0; x.IncrementSubscripts(at)) {
           auto scalar{x.At(at)}; // this is a std string; size() in chars
@@ -76,7 +80,7 @@ class InitialImage {
               (scalarBytes > elementBytes && elements != 0)) {
             return SizeMismatch;
           }
-          std::memcpy(&data_[offset], scalar.data(), elementBytes);
+          std::memcpy(&data_.at(offset), scalar.data(), elementBytes);
           offset += elementBytes;
         }
         return Ok;

diff  --git a/flang/lib/Evaluate/initial-image.cpp b/flang/lib/Evaluate/initial-image.cpp
index 298c1f7c4f65f..4ff4adb146c1f 100644
--- a/flang/lib/Evaluate/initial-image.cpp
+++ b/flang/lib/Evaluate/initial-image.cpp
@@ -24,8 +24,7 @@ auto InitialImage::Add(ConstantSubscript offset, std::size_t bytes,
       return SizeMismatch;
     } else {
       auto at{x.lbounds()};
-      for (auto elements{TotalElementCount(x.shape())}; elements-- > 0;
-           x.IncrementSubscripts(at)) {
+      for (; elements-- > 0; x.IncrementSubscripts(at)) {
         auto scalar{x.At(at)};
         // TODO: length type parameter values?
         for (const auto &[symbolRef, indExpr] : scalar) {
@@ -38,7 +37,7 @@ auto InitialImage::Add(ConstantSubscript offset, std::size_t bytes,
             Result added{Add(offset + component.offset(), component.size(),
                 indExpr.value(), context)};
             if (added != Ok) {
-              return Ok;
+              return added;
             }
           }
         }


        


More information about the flang-commits mailing list