Index: webrtc/base/copyonwritebuffer.h |
diff --git a/webrtc/base/copyonwritebuffer.h b/webrtc/base/copyonwritebuffer.h |
index 17f2710f3ecbd404646268d4af9a003d7c2e5465..87f24bf51d6db78aecc44442c810d0d0887d8dec 100644 |
--- a/webrtc/base/copyonwritebuffer.h |
+++ b/webrtc/base/copyonwritebuffer.h |
@@ -42,7 +42,9 @@ class CopyOnWriteBuffer { |
template <typename T, typename internal::ByteType<T>::t = 0> |
CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) |
: CopyOnWriteBuffer(size, capacity) { |
- std::memcpy(buffer_->data(), data, size); |
+ if (buffer_) { |
+ std::memcpy(buffer_->data(), data, size); |
+ } |
} |
// Construct a buffer from the contents of an array. |
@@ -123,13 +125,24 @@ class CopyOnWriteBuffer { |
return !(*this == buf); |
} |
+ uint8_t& operator[](size_t index) { |
+ RTC_DCHECK_LT(index, size()); |
+ return data()[index]; |
+ } |
+ |
+ uint8_t operator[](size_t index) const { |
+ RTC_DCHECK_LT(index, size()); |
+ return cdata()[index]; |
+ } |
+ |
// Replace the contents of the buffer. Accepts the same types as the |
// constructors. |
template <typename T, typename internal::ByteType<T>::t = 0> |
void SetData(const T* data, size_t size) { |
RTC_DCHECK(IsConsistent()); |
if (!buffer_ || !buffer_->HasOneRef()) { |
- buffer_ = new RefCountedObject<Buffer>(data, size, size); |
+ buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) |
+ : nullptr; |
} else { |
buffer_->SetData(data, size); |
} |