[flang-commits] [PATCH] D127027: [flang] Don't crash on initialization with a zero-sized derived type
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Fri Jun 3 16:05:02 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.
Avoid calls to memcpy with zero byte counts if their address argument
calculations may not be valid expressions.
https://reviews.llvm.org/D127027
Files:
flang/include/flang/Evaluate/initial-image.h
flang/lib/Evaluate/initial-image.cpp
Index: flang/lib/Evaluate/initial-image.cpp
===================================================================
--- flang/lib/Evaluate/initial-image.cpp
+++ flang/lib/Evaluate/initial-image.cpp
@@ -24,8 +24,7 @@
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 @@
Result added{Add(offset + component.offset(), component.size(),
indExpr.value(), context)};
if (added != Ok) {
- return Ok;
+ return added;
}
}
}
Index: flang/include/flang/Evaluate/initial-image.h
===================================================================
--- flang/include/flang/Evaluate/initial-image.h
+++ flang/include/flang/Evaluate/initial-image.h
@@ -49,6 +49,8 @@
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 @@
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 @@
(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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127027.434186.patch
Type: text/x-patch
Size: 2095 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220603/d84f1d14/attachment.bin>
More information about the flang-commits
mailing list