Skip to content

Commit

Permalink
add testing for overflow in read_segment_table
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Nov 15, 2023
1 parent aa27f26 commit da19dd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ jobs:
- name: Test no default features
run: cargo miri test --package capnp --package capnpc-test --no-default-features

- name: Test default features 32-bit
run: cargo miri test --package capnp --package capnpc-test --target i686-unknown-linux-gnu

- name: Test no default features 32-bit
run: cargo miri test --package capnp --package capnpc-test --no-default-features --target i686-unknown-linux-gnu

- name: Test sync_reader
run: cargo miri test --package capnp --package capnpc-test --features sync_reader

Expand Down
18 changes: 15 additions & 3 deletions capnp/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,13 @@ impl SegmentLengthsBuilder {
/// the segment with ID `n`. If the segment overflows the total word count, then this returns
/// a MessageSizeOverflow error.
pub fn try_push_segment(&mut self, length_in_words: usize) -> Result<()> {
self.segment_indices
.push((self.total_words, self.total_words + length_in_words));
self.total_words = self
let new_total_words = self
.total_words
.checked_add(length_in_words)
.ok_or_else(|| Error::from_kind(ErrorKind::MessageSizeOverflow))?;
self.segment_indices
.push((self.total_words, new_total_words));
self.total_words = new_total_words;
Ok(())
}

Expand Down Expand Up @@ -885,6 +886,17 @@ pub mod test {
buf.clear();
}

#[test]
fn test_read_segment_table_overflow() {
let mut buf = vec![];

buf.extend([1, 0, 0, 0]); // 2 segments
buf.extend([0xff, 0xff, 0xff, 0xff]); // 2^32 - 1 words
buf.extend([2, 0, 0, 0]); // 2 words
buf.extend([0, 0, 0, 0]); // padding
assert!(read_segment_table(&mut &buf[..], message::ReaderOptions::new()).is_err());
}

#[test]
fn test_write_segment_table() {
let mut buf = vec![];
Expand Down

0 comments on commit da19dd4

Please sign in to comment.