From 43e5937bf65968f5cb4b17f2994dd65df849a7f3 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 24 Jan 2023 11:59:07 -0800 Subject: [PATCH] Fix bool parser for map entries to look at the whole 64-bit varint and not just the first 32 bits. This is the expected narrowing for bool fields. PiperOrigin-RevId: 504338621 --- src/google/protobuf/map_test.inc | 32 ++++++++++++++++++++++++++ src/google/protobuf/map_type_handler.h | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/map_test.inc b/src/google/protobuf/map_test.inc index 14146d8afd..6d1a70cd2b 100644 --- a/src/google/protobuf/map_test.inc +++ b/src/google/protobuf/map_test.inc @@ -76,6 +76,9 @@ // Must be included last. #include "google/protobuf/port_def.inc" +using ::testing::ElementsAre; +using ::testing::Pair; + namespace google { namespace protobuf { @@ -3639,6 +3642,35 @@ TEST(WireFormatForMapFieldTest, MapParseHelpers) { } } +std::string WriteVarint(int number, uint64_t v) { + uint8_t buf[16]; + return std::string(buf, WireFormatLite::WriteUInt64ToArray(number, v, buf)); +} + +std::string WriteString(int number, const std::string& str) { + uint8_t buf[100]; + return std::string(buf, WireFormatLite::WriteStringToArray(number, str, buf)); +} + +TEST(WireFormatForMapFieldTest, BoolWorksWithOverlongValues) { + // map map_bool_bool = 13; + for (uint64_t v : {uint64_t{1}, uint64_t{1000}, uint64_t{100000}, + uint64_t{1} << 32, uint64_t{1} << 63}) { + SCOPED_TRACE(v); + std::string payload = + WriteString(13, WriteVarint(1, v) + WriteVarint(2, v)); + UNITTEST::TestMap obj; + ASSERT_TRUE(obj.ParseFromString(payload)); + EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true))); + + io::ArrayInputStream raw_input(payload.data(), payload.size()); + io::CodedInputStream input(&raw_input); + obj.Clear(); + ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &obj)); + EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true))); + } +} + // Deterministic Serialization Test ========================================== template diff --git a/src/google/protobuf/map_type_handler.h b/src/google/protobuf/map_type_handler.h index 8979734b0b..2017cb1821 100644 --- a/src/google/protobuf/map_type_handler.h +++ b/src/google/protobuf/map_type_handler.h @@ -434,7 +434,7 @@ inline const char* ReadENUM(const char* ptr, E* value) { return ptr; } inline const char* ReadBOOL(const char* ptr, bool* value) { - *value = static_cast(ReadVarint32(&ptr)); + *value = static_cast(ReadVarint64(&ptr)); return ptr; }