From 005159f1188f3be1b0b9d082cae8192d1a69057e Mon Sep 17 00:00:00 2001 From: king6cong Date: Tue, 8 Jan 2019 21:49:37 +0800 Subject: [PATCH] Fix compute_size(value: &i32) for negative values --- protobuf-test/src/common/v2/test_map_simple.rs | 13 +++++++++++++ .../src/common/v2/test_map_simple_pb.proto | 2 +- protobuf/src/reflect/types.rs | 4 ++++ protobuf/src/stream.rs | 4 ++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/protobuf-test/src/common/v2/test_map_simple.rs b/protobuf-test/src/common/v2/test_map_simple.rs index 9ad83bdc2..a9c0ac81a 100644 --- a/protobuf-test/src/common/v2/test_map_simple.rs +++ b/protobuf-test/src/common/v2/test_map_simple.rs @@ -22,6 +22,19 @@ fn test_map() { test_serialize_deserialize_no_hex(&map); } +#[test] +fn test_map_negtive_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(); 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) } diff --git a/protobuf/src/stream.rs b/protobuf/src/stream.rs index e51ffae81..9fa3e4843 100644 --- a/protobuf/src/stream.rs +++ b/protobuf/src/stream.rs @@ -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.