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}
This commit is contained in:
Mirko Bonadei 2022-08-17 14:24:46 +00:00 committed by WebRTC LUCI CQ
parent b9201b0ef4
commit 7103e7085f

View file

@ -106,7 +106,10 @@ class BufferT {
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 @@ class BufferT {
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);