diff --git a/protobuf-test/src/common/v2/test_map_simple.rs b/protobuf-test/src/common/v2/test_map_simple.rs index 9ad83bdc2..448e3fb68 100644 --- a/protobuf-test/src/common/v2/test_map_simple.rs +++ b/protobuf-test/src/common/v2/test_map_simple.rs @@ -22,6 +22,13 @@ fn test_map() { test_serialize_deserialize_no_hex(&map); } +#[test] +fn test_map_negative_i32_value() { + let mut map = TestMap::new(); + map.mut_m().insert("two".to_owned(), -2); + test_serialize_deserialize("0a 10 0a 03 74 77 6f 10 fe ff ff ff ff ff ff ff ff 01", &map); +} + #[test] fn test_map_with_object() { let mut map = TestMap::new(); diff --git a/protobuf-test/src/common/v2/test_map_simple_pb.proto b/protobuf-test/src/common/v2/test_map_simple_pb.proto index 9835b1463..5654ee9aa 100644 --- a/protobuf-test/src/common/v2/test_map_simple_pb.proto +++ b/protobuf-test/src/common/v2/test_map_simple_pb.proto @@ -5,7 +5,7 @@ option (rustproto.generate_accessors_all) = true; message TestMap { - map m = 1; + map m = 1; map mm = 2; } diff --git a/protobuf/src/reflect/types.rs b/protobuf/src/reflect/types.rs index 14b9f8e3c..f416d2b0e 100644 --- a/protobuf/src/reflect/types.rs +++ b/protobuf/src/reflect/types.rs @@ -222,6 +222,10 @@ impl ProtobufType for ProtobufTypeInt32 { } fn compute_size(value: &i32) -> u32 { + // See also: https://github.com/protocolbuffers/protobuf/blob/bd00671b924310c0353a730bf8fa77c44e0a9c72/src/google/protobuf/io/coded_stream.h#L1300-L1306 + if *value < 0 { + return 10 + } rt::compute_raw_varint32_size(*value as u32) }