Skip to content

Commit

Permalink
Replace a memcpy with std::copy_n
Browse files Browse the repository at this point in the history
memcpy has a bug where it doesn't work with empty slices whose pointer
is null. C++ functions in <algorithm> have this bug fixed and, in a good
STL, will specialize down to memcpy or memmove anyway.

This fixes a bunch of UBSan failures in Chromium, such as
https://luci-milo.appspot.com/ui/inv/build-8752767322372882913/test-results?q=RTCEncodedVideoFrameTest.ConstructorCopiesMetadata&sortby=&groupby=

See https://davidben.net/2024/01/15/empty-slices.html

Bug: chromium:40248746
Change-Id: Ibfb9c4d7b44df53766a16e40fabd0a374140d89c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/344260
Auto-Submit: David Benjamin <[email protected]>
Commit-Queue: Harald Alvestrand <[email protected]>
Reviewed-by: Harald Alvestrand <[email protected]>
Cr-Commit-Position: refs/heads/main@{#41989}
  • Loading branch information
davidben authored and WebRTC LUCI CQ committed Apr 3, 2024
1 parent 80256a0 commit abf1e0b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions api/video/encoded_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include "api/video/encoded_image.h"

#include <stdlib.h>
#include <string.h>

#include <algorithm>

namespace webrtc {

Expand All @@ -21,7 +22,7 @@ EncodedImageBuffer::EncodedImageBuffer(size_t size) : size_(size) {

EncodedImageBuffer::EncodedImageBuffer(const uint8_t* data, size_t size)
: EncodedImageBuffer(size) {
memcpy(buffer_, data, size);
std::copy_n(data, size, buffer_);
}

EncodedImageBuffer::~EncodedImageBuffer() {
Expand Down

0 comments on commit abf1e0b

Please sign in to comment.