Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions tonic/src/codec/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ where
let offset = buf.len();

buf.reserve(HEADER_SIZE);
unsafe {
buf.advance_mut(HEADER_SIZE);
}
buf.put_slice(&[0u8; HEADER_SIZE]);

if let Some(encoding) = compression_encoding {
uncompression_buf.clear();
Expand Down Expand Up @@ -399,3 +397,56 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::panic::catch_unwind;

struct PanickingEncoder;

impl Encoder for PanickingEncoder {
type Item = String;
type Error = Status;

fn encode(
&mut self,
_item: Self::Item,
_dst: &mut EncodeBuf<'_>,
) -> Result<(), Self::Error> {
panic!("encoder deliberate panic for testing exception safety");
}
}

#[test]
fn encode_item_exception_safety_on_panic() {
let mut encoder = PanickingEncoder;
let mut buf = BytesMut::new();
let mut uncompression_buf = BytesMut::new();

let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
encode_item(
&mut encoder,
&mut buf,
&mut uncompression_buf,
None,
None,
BufferSettings::default(),
"test".to_string(),
)
}));

assert!(result.is_err(), "Encoder panic should unwind correctly.");
// Buffer must only contain initialized bytes (5 zero bytes written by put_slice).
assert_eq!(
buf.len(),
HEADER_SIZE,
"Buffer length should reflect reserved header bytes."
);
assert_eq!(
&buf[..],
&[0u8; HEADER_SIZE],
"Buffer must contain only initialized zero bytes."
);
}
}
Loading