1
Fork 0

Auto merge of #132791 - tyilo:big-file-fail-fast, r=compiler-errors

rustc: Fail fast when compiling a source file larger than 4 GiB

Currently if you try to compile a file that is larger than 4 GiB, `rustc` will first read the whole into memory before failing.

If we can read the metadata of the file, we can fail before reading the file.
This commit is contained in:
bors 2024-11-24 07:37:32 +00:00
commit ab3cf268b5
2 changed files with 15 additions and 1 deletions

View file

@ -1829,6 +1829,8 @@ impl StableSourceFileId {
} }
impl SourceFile { impl SourceFile {
const MAX_FILE_SIZE: u32 = u32::MAX - 1;
pub fn new( pub fn new(
name: FileName, name: FileName,
mut src: String, mut src: String,
@ -1849,6 +1851,9 @@ impl SourceFile {
let stable_id = StableSourceFileId::from_filename_in_current_crate(&name); let stable_id = StableSourceFileId::from_filename_in_current_crate(&name);
let source_len = src.len(); let source_len = src.len();
let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?; let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?;
if source_len > Self::MAX_FILE_SIZE {
return Err(OffsetOverflowError);
}
let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src); let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src);

View file

@ -115,6 +115,12 @@ impl FileLoader for RealFileLoader {
} }
fn read_file(&self, path: &Path) -> io::Result<String> { fn read_file(&self, path: &Path) -> io::Result<String> {
if path.metadata().is_ok_and(|metadata| metadata.len() > SourceFile::MAX_FILE_SIZE.into()) {
return Err(io::Error::other(format!(
"text files larger than {} bytes are unsupported",
SourceFile::MAX_FILE_SIZE
)));
}
fs::read_to_string(path) fs::read_to_string(path)
} }
@ -297,7 +303,10 @@ impl SourceMap {
/// unmodified. /// unmodified.
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> { pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| { self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
eprintln!("fatal error: rustc does not support files larger than 4GB"); eprintln!(
"fatal error: rustc does not support text files larger than {} bytes",
SourceFile::MAX_FILE_SIZE
);
crate::fatal_error::FatalError.raise() crate::fatal_error::FatalError.raise()
}) })
} }