Skip to content

Commit c3a6062

Browse files
committed
PR feedback
1 parent 95150d7 commit c3a6062

File tree

10 files changed

+27
-20
lines changed

10 files changed

+27
-20
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl CodegenResults {
266266
});
267267
}
268268

269-
let Some(mut decoder) = MemDecoder::new(&data[4..], 0) else {
269+
let Ok(mut decoder) = MemDecoder::new(&data[4..], 0) else {
270270
return Err(CodegenErrors::CorruptFile);
271271
};
272272
let rustc_version = decoder.read_str();

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
654654
})
655655
}
656656
CodegenErrors::CorruptFile => {
657-
dcx.emit_fatal(RlinkCorruptFile { file: file.display().to_string() });
657+
dcx.emit_fatal(RlinkCorruptFile { file });
658658
}
659659
};
660660
}

compiler/rustc_driver_impl/src/session_diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub(crate) struct RlinkNotAFile;
3434

3535
#[derive(Diagnostic)]
3636
#[diag(driver_impl_rlink_corrupt_file)]
37-
pub(crate) struct RlinkCorruptFile {
38-
pub file: String,
37+
pub(crate) struct RlinkCorruptFile<'a> {
38+
pub file: &'a std::path::Path,
3939
}
4040

4141
#[derive(Diagnostic)]

compiler/rustc_incremental/src/persist/load.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
115115

116116
if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
117117
// Decode the list of work_products
118-
let Some(mut work_product_decoder) =
118+
let Ok(mut work_product_decoder) =
119119
MemDecoder::new(&work_products_data[..], start_pos)
120120
else {
121121
sess.dcx().emit_warn(errors::CorruptFile { path: &work_products_path });
@@ -150,7 +150,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
150150
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
151151
LoadResult::LoadDepGraph(path, err) => LoadResult::LoadDepGraph(path, err),
152152
LoadResult::Ok { data: (bytes, start_pos) } => {
153-
let Some(mut decoder) = MemDecoder::new(&bytes, start_pos) else {
153+
let Ok(mut decoder) = MemDecoder::new(&bytes, start_pos) else {
154154
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
155155
return LoadResult::DataOutOfDate;
156156
};
@@ -192,7 +192,7 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {
192192
let path = query_cache_path(sess);
193193
match load_data(&path, sess) {
194194
LoadResult::Ok { data: (bytes, start_pos) } => {
195-
let cache = OnDiskCache::new(sess, bytes, start_pos).unwrap_or_else(|| {
195+
let cache = OnDiskCache::new(sess, bytes, start_pos).unwrap_or_else(|()| {
196196
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
197197
OnDiskCache::new_empty(sess.source_map())
198198
});

compiler/rustc_metadata/src/locator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ fn get_metadata_section<'p>(
853853
slice_owned(mmap, Deref::deref)
854854
}
855855
};
856-
let Some(blob) = MetadataBlob::new(raw_bytes) else {
856+
let Ok(blob) = MetadataBlob::new(raw_bytes) else {
857857
return Err(MetadataError::LoadFailure(format!(
858858
"corrupt metadata encountered in {}",
859859
filename.display()

compiler/rustc_metadata/src/rmeta/decoder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ impl std::ops::Deref for MetadataBlob {
5454
}
5555

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

62+
/// Since this has passed the validation of [`MetadataBlob::new`], this returns bytes which are
63+
/// known to pass the [`MemDecoder`] validation.
6164
pub fn bytes(&self) -> &OwnedSlice {
6265
&self.0
6366
}

compiler/rustc_middle/src/query/on_disk_cache.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ impl EncodedSourceFileId {
154154

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

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

172-
Some(Self {
175+
Ok(Self {
173176
serialized_data: RwLock::new(Some(data)),
174177
file_index_to_stable_id: footer.file_index_to_stable_id,
175178
file_index_to_file: Default::default(),

compiler/rustc_serialize/src/opaque.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::int_overflow::DebugStrictAdd;
1717

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

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

2222
/// The size of the buffer in `FileEncoder`.
2323
const BUF_SIZE: usize = 8192;
@@ -183,7 +183,7 @@ impl FileEncoder {
183183
}
184184

185185
pub fn finish(&mut self) -> FileEncodeResult {
186-
self.write_all(FOOTER);
186+
self.write_all(MAGIC_END_BYTES);
187187
self.flush();
188188
#[cfg(debug_assertions)]
189189
{
@@ -264,10 +264,10 @@ pub struct MemDecoder<'a> {
264264

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

273273
#[inline]

compiler/rustc_serialize/tests/leb128.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_serialize::leb128::*;
22
use rustc_serialize::opaque::MemDecoder;
3+
use rustc_serialize::opaque::MAGIC_END_BYTES;
34
use rustc_serialize::Decoder;
45

56
macro_rules! impl_test_unsigned_leb128 {
@@ -27,7 +28,7 @@ macro_rules! impl_test_unsigned_leb128 {
2728
stream.extend(&buf[..n]);
2829
}
2930
let stream_end = stream.len();
30-
stream.extend(b"rust-end-file");
31+
stream.extend(MAGIC_END_BYTES);
3132

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

8182
let mut decoder = MemDecoder::new(&stream, 0).unwrap();
8283
for &expected in &values {

src/librustdoc/scrape_examples.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub(crate) fn load_call_locations(
344344
Ok(bytes) => bytes,
345345
Err(e) => dcx.fatal(format!("failed to load examples: {e}")),
346346
};
347-
let Some(mut decoder) = MemDecoder::new(&bytes, 0) else {
347+
let Ok(mut decoder) = MemDecoder::new(&bytes, 0) else {
348348
dcx.fatal(format!("Corrupt metadata encountered in {path}"))
349349
};
350350
let calls = AllCallLocations::decode(&mut decoder);

0 commit comments

Comments
 (0)