Check that data_ is non-null before memcpy in EnsureCapacityWithHeadroom

Since we've passed IsConsistent(), if data_ is null, size_ must be
zero, so we might attempt to copy zero bytes from a nullptr.  This does
not seem to cause problems in practice, but is still undefined
behaviour. This was caught on an UBsan test run in Firefox.

Bug: webrtc:11613
Change-Id: Iad795bf19ed69b56e066958a54a7e3a434b996cf
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176280
Commit-Queue: Dan Minor <dminor@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31386}
This commit is contained in:
Dan Minor
2020-05-28 09:21:42 -04:00
committed by Commit Bot
parent f1393e23a2
commit b164e70450

View File

@ -370,7 +370,9 @@ class BufferT {
: capacity; : capacity;
std::unique_ptr<T[]> new_data(new T[new_capacity]); std::unique_ptr<T[]> new_data(new T[new_capacity]);
if (data_ != nullptr) {
std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T)); std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
}
MaybeZeroCompleteBuffer(); MaybeZeroCompleteBuffer();
data_ = std::move(new_data); data_ = std::move(new_data);
capacity_ = new_capacity; capacity_ = new_capacity;