Skip to content

Commit 43e5937

Browse files
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
1 parent 8c6dcd0 commit 43e5937

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/google/protobuf/map_test.inc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
// Must be included last.
7777
#include "google/protobuf/port_def.inc"
7878

79+
using ::testing::ElementsAre;
80+
using ::testing::Pair;
81+
7982
namespace google {
8083
namespace protobuf {
8184

@@ -3639,6 +3642,35 @@ TEST(WireFormatForMapFieldTest, MapParseHelpers) {
36393642
}
36403643
}
36413644

3645+
std::string WriteVarint(int number, uint64_t v) {
3646+
uint8_t buf[16];
3647+
return std::string(buf, WireFormatLite::WriteUInt64ToArray(number, v, buf));
3648+
}
3649+
3650+
std::string WriteString(int number, const std::string& str) {
3651+
uint8_t buf[100];
3652+
return std::string(buf, WireFormatLite::WriteStringToArray(number, str, buf));
3653+
}
3654+
3655+
TEST(WireFormatForMapFieldTest, BoolWorksWithOverlongValues) {
3656+
// map<bool, bool> map_bool_bool = 13;
3657+
for (uint64_t v : {uint64_t{1}, uint64_t{1000}, uint64_t{100000},
3658+
uint64_t{1} << 32, uint64_t{1} << 63}) {
3659+
SCOPED_TRACE(v);
3660+
std::string payload =
3661+
WriteString(13, WriteVarint(1, v) + WriteVarint(2, v));
3662+
UNITTEST::TestMap obj;
3663+
ASSERT_TRUE(obj.ParseFromString(payload));
3664+
EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true)));
3665+
3666+
io::ArrayInputStream raw_input(payload.data(), payload.size());
3667+
io::CodedInputStream input(&raw_input);
3668+
obj.Clear();
3669+
ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &obj));
3670+
EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true)));
3671+
}
3672+
}
3673+
36423674
// Deterministic Serialization Test ==========================================
36433675

36443676
template <typename T>

src/google/protobuf/map_type_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ inline const char* ReadENUM(const char* ptr, E* value) {
434434
return ptr;
435435
}
436436
inline const char* ReadBOOL(const char* ptr, bool* value) {
437-
*value = static_cast<bool>(ReadVarint32(&ptr));
437+
*value = static_cast<bool>(ReadVarint64(&ptr));
438438
return ptr;
439439
}
440440

0 commit comments

Comments
 (0)