Fix UBSan issues in QUICHE It is UB to pass `nullptr` to `memcpy` and friends, even when the byte count is zero. This is a language bug in C that got carried over to C++. UBSan flags these as issues, which is one of the blockers for running Chromium through UBSan. `std::copy` and friends from `<algorithm>` do not have this issue, so use those instead. PiperOrigin-RevId: 576257137
diff --git a/quiche/common/quiche_data_writer.cc b/quiche/common/quiche_data_writer.cc index 5e79439..98b5c37 100644 --- a/quiche/common/quiche_data_writer.cc +++ b/quiche/common/quiche_data_writer.cc
@@ -99,7 +99,8 @@ return false; } - memcpy(dest, data, data_len); + std::copy(static_cast<const char*>(data), + static_cast<const char*>(data) + data_len, dest); length_ += data_len; return true; @@ -111,7 +112,7 @@ return false; } - memset(dest, byte, count); + std::fill(dest, dest + count, byte); length_ += count; return true; @@ -122,7 +123,7 @@ if (length_ > capacity_) { return; } - memset(buffer_ + length_, 0x00, capacity_ - length_); + std::fill(buffer_ + length_, buffer_ + capacity_, 0x00); length_ = capacity_; }
diff --git a/quiche/quic/core/quic_packets.cc b/quiche/quic/core/quic_packets.cc index 2262877..907c7c8 100644 --- a/quiche/quic/core/quic_packets.cc +++ b/quiche/quic/core/quic_packets.cc
@@ -4,6 +4,7 @@ #include "quiche/quic/core/quic_packets.h" +#include <algorithm> #include <utility> #include "absl/strings/escaping.h" @@ -311,7 +312,7 @@ std::unique_ptr<QuicEncryptedPacket> QuicEncryptedPacket::Clone() const { char* buffer = new char[this->length()]; - memcpy(buffer, this->data(), this->length()); + std::copy(this->data(), this->data() + this->length(), buffer); return std::make_unique<QuicEncryptedPacket>(buffer, this->length(), true); }