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
pull/15151/head
Adam Cozzette 1 year ago committed by Copybara-Service
parent 1250d5f6cc
commit bc66a18040
  1. 5
      src/google/protobuf/lite_unittest.cc
  2. 1
      src/google/protobuf/parse_context.h

@ -8,6 +8,7 @@
// Author: kenton@google.com (Kenton Varda)
#include <climits>
#include <cstdint>
#include <iostream>
#include <limits>
#include <string>
@ -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;

@ -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 <typename T>
PROTOBUF_NODISCARD const char* VarintParse(const char* p, T* out) {
#if defined(__aarch64__) && defined(ABSL_IS_LITTLE_ENDIAN)

Loading…
Cancel
Save