Skip to content

Commit

Permalink
Advance pos_of_buffer_start when writing directly to a writer
Browse files Browse the repository at this point in the history
If a message is bigger than the buffer, it will be written
directly to the writer, and there was a bug in that case where
pos_of_buffer_start was not advanced. This causes later
assertions to fail, such as the one in
Message::write_length_delimited_to, which asserts based on
total_bytes_written(), which is in turn based on
pos_of_buffer_start.
  • Loading branch information
Colecf authored and stepancheg committed Jun 26, 2024
1 parent 639579d commit c0a3b0d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions protobuf/src/coded_output_stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl<'a> CodedOutputStream<'a> {
}
OutputTarget::Write(ref mut write, _) => {
write.write_all(bytes)?;
self.pos_of_buffer_start += bytes.len() as u64;
}
OutputTarget::Vec(ref mut vec) => {
assert!(self.buffer.pos_within_buf() == 0);
Expand Down Expand Up @@ -1223,4 +1224,15 @@ mod test {
assert_eq!((i + 1) * 3, stream.total_bytes_written());
}
}

#[test]
fn total_bytes_written_updated_when_writing_lots_of_bytes() {
let data = "ff".repeat(10000);
let bytes = decode_hex(&data);
test_write(&data, |os| {
os.write_raw_bytes(&bytes)?;
assert_eq!(os.total_bytes_written(), 10000);
Ok(())
});
}
}

0 comments on commit c0a3b0d

Please sign in to comment.