Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compute_size(value: &i32) for negative number #372

Merged
merged 1 commit into from Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A side note: length computation logic is duplicated between this function and call to i32::len_varint(). That code needs clean up (deduplication), but it may be not trivial, and I will do it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

rt::compute_raw_varint32_size(*value as u32)
}

Expand Down