PR feedback

This commit is contained in:
Ben Kimock 2024-05-21 20:12:07 -04:00
parent 95150d7246
commit c3a606237d
10 changed files with 27 additions and 20 deletions

View File

@ -266,7 +266,7 @@ pub fn deserialize_rlink(
});
}
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();

View File

@ -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 });
}
};
}

View File

@ -34,8 +34,8 @@ pub(crate) struct RLinkRustcVersionMismatch<'a> {
#[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)]

View File

@ -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 });
@ -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;
};
@ -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())
});

View File

@ -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()

View File

@ -54,10 +54,13 @@ fn deref(&self) -> &[u8] {
}
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
}

View File

@ -154,7 +154,10 @@ fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> 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)?;
@ -169,7 +172,7 @@ pub fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Option<Self> {
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(),

View File

@ -17,7 +17,7 @@
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;
@ -183,7 +183,7 @@ fn panic_invalid_write<const N: usize>(written: usize) {
}
pub fn finish(&mut self) -> FileEncodeResult {
self.write_all(FOOTER);
self.write_all(MAGIC_END_BYTES);
self.flush();
#[cfg(debug_assertions)]
{
@ -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]

View File

@ -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 {
@ -27,7 +28,7 @@ fn $test_name() {
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 {
@ -76,7 +77,7 @@ fn $test_name() {
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 {

View File

@ -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);