File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -1843,6 +1843,8 @@ impl StableSourceFileId {
1843
1843
}
1844
1844
1845
1845
impl SourceFile {
1846
+ const MAX_FILE_SIZE : u32 = u32:: MAX - 1 ;
1847
+
1846
1848
pub fn new (
1847
1849
name : FileName ,
1848
1850
mut src : String ,
@@ -1863,6 +1865,9 @@ impl SourceFile {
1863
1865
let stable_id = StableSourceFileId :: from_filename_in_current_crate ( & name) ;
1864
1866
let source_len = src. len ( ) ;
1865
1867
let source_len = u32:: try_from ( source_len) . map_err ( |_| OffsetOverflowError ) ?;
1868
+ if source_len > Self :: MAX_FILE_SIZE {
1869
+ return Err ( OffsetOverflowError ) ;
1870
+ }
1866
1871
1867
1872
let ( lines, multibyte_chars) = analyze_source_file:: analyze_source_file ( & src) ;
1868
1873
Original file line number Diff line number Diff line change @@ -115,6 +115,12 @@ impl FileLoader for RealFileLoader {
115
115
}
116
116
117
117
fn read_file ( & self , path : & Path ) -> io:: Result < String > {
118
+ if path. metadata ( ) . is_ok_and ( |metadata| metadata. len ( ) > SourceFile :: MAX_FILE_SIZE . into ( ) ) {
119
+ return Err ( io:: Error :: other ( format ! (
120
+ "text files larger than {} bytes are unsupported" ,
121
+ SourceFile :: MAX_FILE_SIZE
122
+ ) ) ) ;
123
+ }
118
124
fs:: read_to_string ( path)
119
125
}
120
126
@@ -297,7 +303,10 @@ impl SourceMap {
297
303
/// unmodified.
298
304
pub fn new_source_file ( & self , filename : FileName , src : String ) -> Lrc < SourceFile > {
299
305
self . try_new_source_file ( filename, src) . unwrap_or_else ( |OffsetOverflowError | {
300
- eprintln ! ( "fatal error: rustc does not support files larger than 4GB" ) ;
306
+ eprintln ! (
307
+ "fatal error: rustc does not support text files larger than {} bytes" ,
308
+ SourceFile :: MAX_FILE_SIZE
309
+ ) ;
301
310
crate :: fatal_error:: FatalError . raise ( )
302
311
} )
303
312
}
You can’t perform that action at this time.
0 commit comments