Prevent UB on BufferT::AppendData and ctor.
While [1] partially fixed the problem, UB was still possible when
AppendData was called with size=0 or in one of the constructors.
[1] - https://webrtc-review.googlesource.com/c/src/+/271502
Bug: webrtc:14292
Change-Id: I9196e23687ee82b7bfbe1ed43460d9f99adcd1ee
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/271980
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37814}
diff --git a/rtc_base/buffer.h b/rtc_base/buffer.h
index 819e7db..6663c68 100644
--- a/rtc_base/buffer.h
+++ b/rtc_base/buffer.h
@@ -106,7 +106,10 @@
internal::BufferCompat<T, U>::value>::type* = nullptr>
BufferT(U* data, size_t size, size_t capacity) : BufferT(size, capacity) {
static_assert(sizeof(T) == sizeof(U), "");
- std::memcpy(data_.get(), data, size * sizeof(U));
+ if (size > 0) {
+ RTC_DCHECK(data);
+ std::memcpy(data_.get(), data, size * sizeof(U));
+ }
}
// Construct a buffer from the contents of an array.
@@ -267,10 +270,10 @@
typename std::enable_if<
internal::BufferCompat<T, U>::value>::type* = nullptr>
void AppendData(const U* data, size_t size) {
- if (!data) {
- RTC_CHECK_EQ(size, 0U);
+ if (size == 0) {
return;
}
+ RTC_DCHECK(data);
RTC_DCHECK(IsConsistent());
const size_t new_size = size_ + size;
EnsureCapacityWithHeadroom(new_size, true);