diff options
| author | bors <bors@rust-lang.org> | 2024-11-24 07:37:32 +0000 | 
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-11-24 07:37:32 +0000 | 
| commit | ab3cf268b518f0312d86346db3bf79d5ec33b3b7 (patch) | |
| tree | dbe8b1dc18a1fdebc3bcc47ee4134ddb22261189 /compiler/rustc_span/src/lib.rs | |
| parent | 4e4c20d2abe6a218991152b735800fbaf51aedd6 (diff) | |
| parent | 5caf516cd087af9b6b8349e12709b2e77b054f39 (diff) | |
| download | rust-ab3cf268b518f0312d86346db3bf79d5ec33b3b7.tar.gz rust-ab3cf268b518f0312d86346db3bf79d5ec33b3b7.zip | |
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.
Diffstat (limited to 'compiler/rustc_span/src/lib.rs')
| -rw-r--r-- | compiler/rustc_span/src/lib.rs | 5 | 
1 files changed, 5 insertions, 0 deletions
| diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 8966cfc9171..1481e1cbd91 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1829,6 +1829,8 @@ impl StableSourceFileId { } impl SourceFile { + const MAX_FILE_SIZE: u32 = u32::MAX - 1; + pub fn new( name: FileName, mut src: String, @@ -1849,6 +1851,9 @@ impl SourceFile { let stable_id = StableSourceFileId::from_filename_in_current_crate(&name); let source_len = src.len(); 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); | 
