Skip to content

Commit c996de3

Browse files
committed
Fix #132862
1 parent 21ed43a commit c996de3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

compiler/rustc_span/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,8 @@ impl StableSourceFileId {
18431843
}
18441844

18451845
impl SourceFile {
1846+
const MAX_FILE_SIZE: u32 = u32::MAX - 1;
1847+
18461848
pub fn new(
18471849
name: FileName,
18481850
mut src: String,
@@ -1863,6 +1865,9 @@ impl SourceFile {
18631865
let stable_id = StableSourceFileId::from_filename_in_current_crate(&name);
18641866
let source_len = src.len();
18651867
let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?;
1868+
if source_len > Self::MAX_FILE_SIZE {
1869+
return Err(OffsetOverflowError);
1870+
}
18661871

18671872
let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src);
18681873

compiler/rustc_span/src/source_map.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ impl FileLoader for RealFileLoader {
115115
}
116116

117117
fn read_file(&self, path: &Path) -> io::Result<String> {
118-
if path.metadata().is_ok_and(|metadata| metadata.len() > u32::MAX.into()) {
119-
return Err(io::Error::other("rustc does not support text files larger than 4 GiB"));
118+
if path.metadata().is_ok_and(|metadata| metadata.len() > SourceFile::MAX_FILE_SIZE.into()) {
119+
return Err(io::Error::other(format!(
120+
"rustc doest not support text files larger than {} bytes",
121+
SourceFile::MAX_FILE_SIZE
122+
)));
120123
}
121124
fs::read_to_string(path)
122125
}
@@ -300,7 +303,10 @@ impl SourceMap {
300303
/// unmodified.
301304
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
302305
self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
303-
eprintln!("fatal error: rustc does not support text files larger than 4 GiB");
306+
eprintln!(
307+
"fatal error: rustc does not support text files larger than {} bytes",
308+
SourceFile::MAX_FILE_SIZE
309+
);
304310
crate::fatal_error::FatalError.raise()
305311
})
306312
}

0 commit comments

Comments
 (0)