Skip to content

Commit

Permalink
Fix panic when string field occurs more than once
Browse files Browse the repository at this point in the history
Also add test

Thanks to `cargo fuzz` which revealed this problem
  • Loading branch information
stepancheg committed Jun 9, 2018
1 parent bb517a6 commit 28adf07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
21 changes: 21 additions & 0 deletions protobuf-test/src/common/v2/test_singular_concat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Test when two messages are concatenated, singular fields are taken from the last one.
use super::test_singular_concat_pb::*;

use protobuf_test_common::*;

#[test]
fn test_concat_bytes() {
let mut m = TestSingularConcat::new();
m.set_b(b"\xdd\xee".to_vec());

test_deserialize("12 03 aa bb cc 12 02 dd ee", &m);
}

#[test]
fn test_concat_string() {
let mut m = TestSingularConcat::new();
m.set_s("\x61\x62".to_string());

test_deserialize("0a 03 21 22 23 0a 02 61 62", &m);
}
8 changes: 8 additions & 0 deletions protobuf-test/src/common/v2/test_singular_concat_pb.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto2";

package test_singular_concar;

message TestSingularConcat {
optional string s = 1;
optional bytes b = 2;
}
3 changes: 1 addition & 2 deletions protobuf/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,7 @@ impl<'a> CodedInputStream<'a> {
}

pub fn read_string_into(&mut self, target: &mut String) -> ProtobufResult<()> {
// assert string is empty, otherwize UTF-8 validation is too expensive
assert!(target.is_empty());
target.clear();
// take target's buffer
let mut vec = mem::replace(target, String::new()).into_bytes();
self.read_bytes_into(&mut vec)?;
Expand Down

0 comments on commit 28adf07

Please sign in to comment.