Skip to content

Clamp instead of asserting in FileEncoder::write_with #116188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 2 additions & 10 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,8 @@ impl FileEncoder {
let buf = unsafe { self.buffer_empty().first_chunk_mut::<N>().unwrap_unchecked() };
let written = visitor(buf);
// We have to ensure that an errant visitor cannot cause self.buffered to exeed BUF_SIZE.
if written > N {
Self::panic_invalid_write::<N>(written);
}
self.buffered += written;
}

#[cold]
#[inline(never)]
fn panic_invalid_write<const N: usize>(written: usize) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't caught it during the original review, but if we'll leave this in, this funciton should probably return !

panic!("FileEncoder::write_with::<{N}> cannot be used to write {written} bytes");
debug_assert!(written <= N);
self.buffered += written.min(N);
}

/// Helper for calls where [`FileEncoder::write_with`] always writes the whole array.
Expand Down