summaryrefslogtreecommitdiffstats
path: root/flang/runtime/buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'flang/runtime/buffer.h')
-rw-r--r--flang/runtime/buffer.h11
1 files changed, 8 insertions, 3 deletions
diff --git a/flang/runtime/buffer.h b/flang/runtime/buffer.h
index a77a5a5dda5c..93fda36f500d 100644
--- a/flang/runtime/buffer.h
+++ b/flang/runtime/buffer.h
@@ -148,10 +148,15 @@ private:
buffer_ =
reinterpret_cast<char *>(AllocateMemoryOrCrash(terminator, size_));
auto chunk{std::min<std::int64_t>(length_, oldSize - start_)};
- std::memcpy(buffer_, old + start_, chunk);
+ // "memcpy" in glibc has a "nonnull" attribute on the source pointer.
+ // Avoid passing a null pointer, since it would result in an undefined
+ // behavior.
+ if (old != nullptr) {
+ std::memcpy(buffer_, old + start_, chunk);
+ std::memcpy(buffer_ + chunk, old, length_ - chunk);
+ FreeMemory(old);
+ }
start_ = 0;
- std::memcpy(buffer_ + chunk, old, length_ - chunk);
- FreeMemory(old);
}
}