Stop calling memcpy when `bytes_to_copy` is 0
This CL adds an if condition to check if `bytes_to_copy` is greater than 0, so that we don't do the memcpy operation if not needed, and remove the undefined behavior.
Context: crbug.com/1335423
PiperOrigin-RevId: 471457682
diff --git a/quiche/quic/core/http/quic_spdy_stream_body_manager.cc b/quiche/quic/core/http/quic_spdy_stream_body_manager.cc
index d1ba5d2..efb32c3 100644
--- a/quiche/quic/core/http/quic_spdy_stream_body_manager.cc
+++ b/quiche/quic/core/http/quic_spdy_stream_body_manager.cc
@@ -106,7 +106,13 @@
const size_t bytes_to_copy =
std::min<size_t>(body.length(), dest_remaining);
- memcpy(dest, body.data(), bytes_to_copy);
+
+ // According to Section 7.1.4 of the C11 standard (ISO/IEC 9899:2011), null
+ // pointers should not be passed to standard library functions.
+ if (bytes_to_copy > 0) {
+ memcpy(dest, body.data(), bytes_to_copy);
+ }
+
bytes_to_consume += bytes_to_copy;
*total_bytes_read += bytes_to_copy;