Prevent UB on BufferT::AppendData.
As per https://en.cppreference.com/w/cpp/string/byte/memcpy, calling
std::memcpy with dest or src as nullptr is UB.
This CL prevents this from happening and is also looking into
why UBSan didn't catch the issue.
Bug: webrtc:14292
Change-Id: I99833f28ac865719d0dcb02c4de00f33a48c3992
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/271502
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37783}
diff --git a/rtc_base/buffer.h b/rtc_base/buffer.h
index be8e22b..819e7db 100644
--- a/rtc_base/buffer.h
+++ b/rtc_base/buffer.h
@@ -267,6 +267,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);
+ return;
+ }
RTC_DCHECK(IsConsistent());
const size_t new_size = size_ + size;
EnsureCapacityWithHeadroom(new_size, true);