From 48ac38ec9b606844a346fb82b9101ba0c2d7be2b Mon Sep 17 00:00:00 2001 From: Mirko Bonadei Date: Mon, 15 Aug 2022 08:20:44 +0000 Subject: [PATCH] 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 Commit-Queue: Mirko Bonadei Cr-Commit-Position: refs/heads/main@{#37783} --- rtc_base/buffer.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rtc_base/buffer.h b/rtc_base/buffer.h index be8e22bf9f..819e7db3a6 100644 --- a/rtc_base/buffer.h +++ b/rtc_base/buffer.h @@ -267,6 +267,10 @@ class BufferT { typename std::enable_if< internal::BufferCompat::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);