rtc::Buffer: Handle move self-assignment
The object should end up in a valid state, just like after being moved
from.
Bug: webrtc:9857
Change-Id: Ia11f9b8e3191ffe749e4a0640cad946038f494a4
Reviewed-on: https://webrtc-review.googlesource.com/c/106701
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25233}
diff --git a/rtc_base/buffer.h b/rtc_base/buffer.h
index 9bf96cd..2707cdf 100644
--- a/rtc_base/buffer.h
+++ b/rtc_base/buffer.h
@@ -152,7 +152,9 @@
RTC_DCHECK(buf.IsConsistent());
size_ = buf.size_;
capacity_ = buf.capacity_;
- data_ = std::move(buf.data_);
+ using std::swap;
+ swap(data_, buf.data_);
+ buf.data_.reset();
buf.OnMovedFrom();
return *this;
}
@@ -399,6 +401,7 @@
// Called when *this has been moved from. Conceptually it's a no-op, but we
// can mutate the state slightly to help subsequent sanity checks catch bugs.
void OnMovedFrom() {
+ RTC_DCHECK(!data_); // Our heap block should have been stolen.
#if RTC_DCHECK_IS_ON
// Ensure that *this is always inconsistent, to provoke bugs.
size_ = 1;