@@ -1725,6 +1725,8 @@ pub struct SourceFile {
17251725 pub start_pos : BytePos ,
17261726 /// The byte length of this source after normalization.
17271727 pub normalized_source_len : RelativeBytePos ,
1728+ /// The byte length of this source before normalization.
1729+ pub unnormalized_source_len : u32 ,
17281730 /// Locations of lines beginnings in the source code.
17291731 pub lines : FreezeLock < SourceFileLines > ,
17301732 /// Locations of multi-byte characters in the source code.
@@ -1749,6 +1751,7 @@ impl Clone for SourceFile {
17491751 external_src : self . external_src . clone ( ) ,
17501752 start_pos : self . start_pos ,
17511753 normalized_source_len : self . normalized_source_len ,
1754+ unnormalized_source_len : self . unnormalized_source_len ,
17521755 lines : self . lines . clone ( ) ,
17531756 multibyte_chars : self . multibyte_chars . clone ( ) ,
17541757 normalized_pos : self . normalized_pos . clone ( ) ,
@@ -1765,6 +1768,7 @@ impl<S: SpanEncoder> Encodable<S> for SourceFile {
17651768 self . checksum_hash . encode ( s) ;
17661769 // Do not encode `start_pos` as it's global state for this session.
17671770 self . normalized_source_len . encode ( s) ;
1771+ self . unnormalized_source_len . encode ( s) ;
17681772
17691773 // We are always in `Lines` form by the time we reach here.
17701774 assert ! ( self . lines. read( ) . is_lines( ) ) ;
@@ -1838,6 +1842,7 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18381842 let src_hash: SourceFileHash = Decodable :: decode ( d) ;
18391843 let checksum_hash: Option < SourceFileHash > = Decodable :: decode ( d) ;
18401844 let normalized_source_len: RelativeBytePos = Decodable :: decode ( d) ;
1845+ let unnormalized_source_len = Decodable :: decode ( d) ;
18411846 let lines = {
18421847 let num_lines: u32 = Decodable :: decode ( d) ;
18431848 if num_lines > 0 {
@@ -1860,6 +1865,7 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18601865 name,
18611866 start_pos : BytePos :: from_u32 ( 0 ) ,
18621867 normalized_source_len,
1868+ unnormalized_source_len,
18631869 src : None ,
18641870 src_hash,
18651871 checksum_hash,
@@ -1959,6 +1965,12 @@ impl SourceFile {
19591965 SourceFileHash :: new_in_memory ( checksum_hash_kind, src. as_bytes ( ) )
19601966 }
19611967 } ) ;
1968+ // Capture the original source length before normalization.
1969+ let unnormalized_source_len = u32:: try_from ( src. len ( ) ) . map_err ( |_| OffsetOverflowError ) ?;
1970+ if unnormalized_source_len > Self :: MAX_FILE_SIZE {
1971+ return Err ( OffsetOverflowError ) ;
1972+ }
1973+
19621974 let normalized_pos = normalize_src ( & mut src) ;
19631975
19641976 let stable_id = StableSourceFileId :: from_filename_in_current_crate ( & name) ;
@@ -1977,6 +1989,7 @@ impl SourceFile {
19771989 external_src : FreezeLock :: frozen ( ExternalSource :: Unneeded ) ,
19781990 start_pos : BytePos :: from_u32 ( 0 ) ,
19791991 normalized_source_len : RelativeBytePos :: from_u32 ( normalized_source_len) ,
1992+ unnormalized_source_len,
19801993 lines : FreezeLock :: frozen ( SourceFileLines :: Lines ( lines) ) ,
19811994 multibyte_chars,
19821995 normalized_pos,
0 commit comments