From bc66a1804021f97d08ab1bc192cdfbafc0bbf5ab Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 19 Dec 2023 15:49:11 -0800 Subject: [PATCH] Update lite_unittest to pad varints before calling `internal::VarintParse()` The `VarintParse()` function is ordinarily called only by the proto parser, which always provides 16 bytes of "slop" space at the end of the buffer. The ARM-specific optimized path takes advantage of this by always reading at least 8 bytes. However, this caused some test failures in msan due to unit tests not providing a sufficient amount of initialized padding. This CL fixes the problem by making sure the unit tests initialize the full 10-byte buffer and adding a comment stating that this is a precondition of the function. PiperOrigin-RevId: 592366291 --- src/google/protobuf/lite_unittest.cc | 5 +++-- src/google/protobuf/parse_context.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index 09f5ec60ccb1..90235827d114 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -8,6 +8,7 @@ // Author: kenton@google.com (Kenton Varda) #include +#include #include #include #include @@ -104,7 +105,7 @@ void SetSomeTypesInEmptyMessageUnknownFields( TEST(ParseVarintTest, Varint32) { auto test_value = [](uint32_t value, int varint_length) { - uint8_t buffer[10]; + uint8_t buffer[10] = {0}; uint8_t* p = io::CodedOutputStream::WriteVarint32ToArray(value, buffer); ASSERT_EQ(p - buffer, varint_length) << "Value = " << value; @@ -131,7 +132,7 @@ TEST(ParseVarintTest, Varint32) { TEST(ParseVarintTest, Varint64) { auto test_value = [](uint64_t value, int varint_length) { - uint8_t buffer[10]; + uint8_t buffer[10] = {0}; uint8_t* p = io::CodedOutputStream::WriteVarint64ToArray(value, buffer); ASSERT_EQ(p - buffer, varint_length) << "Value = " << value; diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h index af48e07e94e0..9292d517feeb 100644 --- a/src/google/protobuf/parse_context.h +++ b/src/google/protobuf/parse_context.h @@ -847,6 +847,7 @@ static const char* VarintParseSlowArm(const char* p, uint64_t* out, } #endif +// The caller must ensure that p points to at least 10 valid bytes. template PROTOBUF_NODISCARD const char* VarintParse(const char* p, T* out) { #if defined(__aarch64__) && defined(ABSL_IS_LITTLE_ENDIAN)