Skip to content

Commit

Permalink
Fix compute_size(value: &i32) for negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
king6cong committed Jan 9, 2019
1 parent 3ba9cb9 commit 7a1524d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions protobuf-test/src/common/v2/test_map_simple.rs
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion protobuf-test/src/common/v2/test_map_simple_pb.proto
Expand Up @@ -5,7 +5,7 @@ option (rustproto.generate_accessors_all) = true;


message TestMap {
map<string, uint32> m = 1;
map<string, int32> m = 1;
map<string, TestMapEntry> mm = 2;
}

Expand Down
4 changes: 4 additions & 0 deletions protobuf/src/reflect/types.rs
Expand Up @@ -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)
}

Expand Down

0 comments on commit 7a1524d

Please sign in to comment.