Skip to content

Commit

Permalink
serialize: don't panic on short buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Jay Lee <[email protected]>
  • Loading branch information
BusyJay committed Sep 17, 2021
1 parent b3fe229 commit 7e8e8e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions protobuf-test/src/common/v2/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::test_basic_pb::*;
use protobuf::descriptor;
use protobuf::reflect;
use protobuf::CodedInputStream;
use protobuf::CodedOutputStream;
use protobuf::Message;
use protobuf::ProtobufEnum;

Expand Down Expand Up @@ -302,3 +303,14 @@ fn test_parse_length_delimited_from_network_smoke() {
assert_eq!(10, test1.get_a());
is.check_eof().expect("check_eof");
}

/// Test if providing a smaller buffer, protobuf can detect and report error.
#[test]
fn test_serialize_too_large_message() {
let mut test1 = Test1::new();
test1.set_a(150);
let len = test1.compute_size();
let mut bytes = vec![0; len as usize - 1];
let mut s = CodedOutputStream::bytes(&mut bytes);
test1.write_to_with_cached_sizes(&mut s).unwrap_err();
}
4 changes: 3 additions & 1 deletion protobuf/src/coded_output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::zigzag::encode_zig_zag_64;
use crate::Message;
use crate::ProtobufEnum;
use crate::ProtobufEnumOrUnknown;
use crate::ProtobufError;
use crate::ProtobufResult;
use crate::UnknownFields;
use crate::UnknownValueRef;
Expand Down Expand Up @@ -134,7 +135,8 @@ impl<'a> CodedOutputStream<'a> {
self.position = 0;
},
OutputTarget::Bytes => {
panic!("refresh_buffer must not be called on CodedOutputStream created from slice");
return Err(ProtobufError::IoError(
io::Error::new(io::ErrorKind::Other, "given slice is too small to serialize the message")));
}
}
Ok(())
Expand Down

0 comments on commit 7e8e8e0

Please sign in to comment.