File tree 2 files changed +14
-3
lines changed
2 files changed +14
-3
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,8 +115,11 @@ 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 ( ) > 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
+ ) ) ) ;
120
123
}
121
124
fs:: read_to_string ( path)
122
125
}
@@ -300,7 +303,10 @@ impl SourceMap {
300
303
/// unmodified.
301
304
pub fn new_source_file ( & self , filename : FileName , src : String ) -> Lrc < SourceFile > {
302
305
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
+ ) ;
304
310
crate :: fatal_error:: FatalError . raise ( )
305
311
} )
306
312
}
You can’t perform that action at this time.
0 commit comments