Skip to content

Commit

Permalink
Fix byte array reading and writing
Browse files Browse the repository at this point in the history
`Read::read` is not guaranteed to fill the whole buffer, and similarly,
`Write::write` is not guaranteed to write everything. This commit
replaces them with `read_exact` and `write_all` respectively, and
removes the corresponding assertions.
  • Loading branch information
pcapriotti authored and stefanwire committed Jan 24, 2024
1 parent 5612e91 commit e1cfcf8
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions tls_codec/src/quic_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,9 @@ mod rw_bytes {
writer.write_all(&length_bytes)?;

// Now serialize the elements
let mut written = 0;
written += writer.write(bytes)?;
writer.write_all(bytes)?;

if !cfg!(fuzzing) {
debug_assert_eq!(
written, content_length,
"{content_length} bytes should have been serialized but {written} were written",
);
}
if written != content_length {
return Err(Error::EncodingError(format!(
"{content_length} bytes should have been serialized but {written} were written",
)));
}
Ok(written + len_len)
Ok(content_length + len_len)
}

impl Serialize for VLBytes {
Expand Down Expand Up @@ -610,19 +598,8 @@ mod rw_bytes {
let mut result = Self {
vec: vec![0u8; length],
};
let read = bytes.read(result.vec.as_mut_slice())?;
if read == length {
return Ok(result);
}
if !cfg!(fuzzing) {
debug_assert_eq!(
read, length,
"Expected to read {length} bytes but {read} were read.",
);
}
Err(Error::DecodingError(format!(
"{read} bytes were read but {length} were expected",
)))
bytes.read_exact(result.vec.as_mut_slice())?;
Ok(result)
}
}

Expand Down

0 comments on commit e1cfcf8

Please sign in to comment.