Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed May 22, 2024
1 parent 95150d7 commit c3a6062
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl CodegenResults {
});
}

let Some(mut decoder) = MemDecoder::new(&data[4..], 0) else {
let Ok(mut decoder) = MemDecoder::new(&data[4..], 0) else {
return Err(CodegenErrors::CorruptFile);
};
let rustc_version = decoder.read_str();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
})
}
CodegenErrors::CorruptFile => {
dcx.emit_fatal(RlinkCorruptFile { file: file.display().to_string() });
dcx.emit_fatal(RlinkCorruptFile { file });
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_driver_impl/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub(crate) struct RlinkNotAFile;

#[derive(Diagnostic)]
#[diag(driver_impl_rlink_corrupt_file)]
pub(crate) struct RlinkCorruptFile {
pub file: String,
pub(crate) struct RlinkCorruptFile<'a> {
pub file: &'a std::path::Path,
}

#[derive(Diagnostic)]
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr

if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
// Decode the list of work_products
let Some(mut work_product_decoder) =
let Ok(mut work_product_decoder) =
MemDecoder::new(&work_products_data[..], start_pos)
else {
sess.dcx().emit_warn(errors::CorruptFile { path: &work_products_path });
Expand Down Expand Up @@ -150,7 +150,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
LoadResult::LoadDepGraph(path, err) => LoadResult::LoadDepGraph(path, err),
LoadResult::Ok { data: (bytes, start_pos) } => {
let Some(mut decoder) = MemDecoder::new(&bytes, start_pos) else {
let Ok(mut decoder) = MemDecoder::new(&bytes, start_pos) else {
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
return LoadResult::DataOutOfDate;
};
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {
let path = query_cache_path(sess);
match load_data(&path, sess) {
LoadResult::Ok { data: (bytes, start_pos) } => {
let cache = OnDiskCache::new(sess, bytes, start_pos).unwrap_or_else(|| {
let cache = OnDiskCache::new(sess, bytes, start_pos).unwrap_or_else(|()| {
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
OnDiskCache::new_empty(sess.source_map())
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ fn get_metadata_section<'p>(
slice_owned(mmap, Deref::deref)
}
};
let Some(blob) = MetadataBlob::new(raw_bytes) else {
let Ok(blob) = MetadataBlob::new(raw_bytes) else {
return Err(MetadataError::LoadFailure(format!(
"corrupt metadata encountered in {}",
filename.display()
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ impl std::ops::Deref for MetadataBlob {
}

impl MetadataBlob {
pub fn new(slice: OwnedSlice) -> Option<Self> {
if MemDecoder::new(&*slice, 0).is_some() { Some(Self(slice)) } else { None }
/// Runs the [`MemDecoder`] validation and if it passes, constructs a new [`MetadataBlob`].
pub fn new(slice: OwnedSlice) -> Result<Self, ()> {
if MemDecoder::new(&slice, 0).is_ok() { Ok(Self(slice)) } else { Err(()) }
}

/// Since this has passed the validation of [`MetadataBlob::new`], this returns bytes which are
/// known to pass the [`MemDecoder`] validation.
pub fn bytes(&self) -> &OwnedSlice {
&self.0
}
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ impl EncodedSourceFileId {

impl<'sess> OnDiskCache<'sess> {
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
pub fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Option<Self> {
///
/// The serialized cache has some basic integrity checks, if those checks indicate that the
/// on-disk data is corrupt, an error is returned.
pub fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Result<Self, ()> {
assert!(sess.opts.incremental.is_some());

let mut decoder = MemDecoder::new(&data, start_pos)?;
Expand All @@ -169,7 +172,7 @@ impl<'sess> OnDiskCache<'sess> {
let footer: Footer =
decoder.with_position(footer_pos, |decoder| decode_tagged(decoder, TAG_FILE_FOOTER));

Some(Self {
Ok(Self {
serialized_data: RwLock::new(Some(data)),
file_index_to_stable_id: footer.file_index_to_stable_id,
file_index_to_file: Default::default(),
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::int_overflow::DebugStrictAdd;

pub type FileEncodeResult = Result<usize, (PathBuf, io::Error)>;

const FOOTER: &[u8] = b"rust-end-file";
pub const MAGIC_END_BYTES: &[u8] = b"rust-end-file";

/// The size of the buffer in `FileEncoder`.
const BUF_SIZE: usize = 8192;
Expand Down Expand Up @@ -183,7 +183,7 @@ impl FileEncoder {
}

pub fn finish(&mut self) -> FileEncodeResult {
self.write_all(FOOTER);
self.write_all(MAGIC_END_BYTES);
self.flush();
#[cfg(debug_assertions)]
{
Expand Down Expand Up @@ -264,10 +264,10 @@ pub struct MemDecoder<'a> {

impl<'a> MemDecoder<'a> {
#[inline]
pub fn new(data: &'a [u8], position: usize) -> Option<MemDecoder<'a>> {
let data = data.strip_suffix(FOOTER)?;
pub fn new(data: &'a [u8], position: usize) -> Result<MemDecoder<'a>, ()> {
let data = data.strip_suffix(MAGIC_END_BYTES).ok_or(())?;
let Range { start, end } = data.as_ptr_range();
Some(MemDecoder { start, current: data[position..].as_ptr(), end, _marker: PhantomData })
Ok(MemDecoder { start, current: data[position..].as_ptr(), end, _marker: PhantomData })
}

#[inline]
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_serialize/tests/leb128.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_serialize::leb128::*;
use rustc_serialize::opaque::MemDecoder;
use rustc_serialize::opaque::MAGIC_END_BYTES;
use rustc_serialize::Decoder;

macro_rules! impl_test_unsigned_leb128 {
Expand Down Expand Up @@ -27,7 +28,7 @@ macro_rules! impl_test_unsigned_leb128 {
stream.extend(&buf[..n]);
}
let stream_end = stream.len();
stream.extend(b"rust-end-file");
stream.extend(MAGIC_END_BYTES);

let mut decoder = MemDecoder::new(&stream, 0).unwrap();
for &expected in &values {
Expand Down Expand Up @@ -76,7 +77,7 @@ macro_rules! impl_test_signed_leb128 {
stream.extend(&buf[..n]);
}
let stream_end = stream.len();
stream.extend(b"rust-end-file");
stream.extend(MAGIC_END_BYTES);

let mut decoder = MemDecoder::new(&stream, 0).unwrap();
for &expected in &values {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/scrape_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub(crate) fn load_call_locations(
Ok(bytes) => bytes,
Err(e) => dcx.fatal(format!("failed to load examples: {e}")),
};
let Some(mut decoder) = MemDecoder::new(&bytes, 0) else {
let Ok(mut decoder) = MemDecoder::new(&bytes, 0) else {
dcx.fatal(format!("Corrupt metadata encountered in {path}"))
};
let calls = AllCallLocations::decode(&mut decoder);
Expand Down

0 comments on commit c3a6062

Please sign in to comment.