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 8f13250
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions protobuf-test/src/common/v2/test_map_simple.rs
Expand Up @@ -22,6 +22,19 @@ fn test_map() {
test_serialize_deserialize_no_hex(&map);
}

#[test]
fn test_map_negative_int() {
let mut map = TestMap::new();
let mut entry = TestMapEntry::new();
entry.set_v(10);

test_serialize_deserialize("", &map);

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
4 changes: 2 additions & 2 deletions protobuf/src/stream.rs
Expand Up @@ -790,14 +790,14 @@ impl<'a> CodedOutputStream<'a> {
self.position = 0;
},
OutputTarget::Bytes => {
panic!("refresh_buffer must not be called on CodedOutputStream create from slice");
panic!("refresh_buffer must not be called on CodedOutputStream created from slice");
}
}
Ok(())
}

/// Flush to buffer to the underlying buffer.
/// Note that `CodedOutputStream` does `flush` in the descructor,
/// Note that `CodedOutputStream` does `flush` in the destructor,
/// however, if `flush` in desctructor fails, then destructor panics
/// and program terminates. So it's advisable to explicitly call flush
/// before destructor.
Expand Down

0 comments on commit 8f13250

Please sign in to comment.