@@ -533,15 +533,17 @@ impl Parser {
533533 } ;
534534 // TODO: thread through `offset: u64` to `BinaryReader`, remove
535535 // the cast here.
536- let mut reader = BinaryReader :: new_with_offset ( data, self . offset as usize ) ;
536+ let starting_offset = self . offset as usize ;
537+ let mut reader = BinaryReader :: new_with_offset ( data, starting_offset) ;
537538 match self . parse_reader ( & mut reader, eof) {
538539 Ok ( payload) => {
539540 // Be sure to update our offset with how far we got in the
540541 // reader
541- self . offset += usize_to_u64 ( reader. position ) ;
542- self . max_size -= usize_to_u64 ( reader. position ) ;
542+ let consumed = reader. original_position ( ) - starting_offset;
543+ self . offset += usize_to_u64 ( consumed) ;
544+ self . max_size -= usize_to_u64 ( consumed) ;
543545 Ok ( Chunk :: Parsed {
544- consumed : reader . position ,
546+ consumed : consumed ,
545547 payload,
546548 } )
547549 }
@@ -595,7 +597,7 @@ impl Parser {
595597 return Ok ( Payload :: End ( reader. original_position ( ) ) ) ;
596598 }
597599
598- let id_pos = reader. position ;
600+ let id_pos = reader. original_position ( ) ;
599601 let id = reader. read_u8 ( ) ?;
600602 if id & 0x80 != 0 {
601603 return Err ( BinaryReaderError :: new ( "malformed section id" , id_pos) ) ;
@@ -608,9 +610,10 @@ impl Parser {
608610 // but it is required for nested modules/components to correctly ensure
609611 // that all sections live entirely within their section of the
610612 // file.
613+ let consumed = reader. original_position ( ) - id_pos;
611614 let section_overflow = self
612615 . max_size
613- . checked_sub ( usize_to_u64 ( reader . position ) )
616+ . checked_sub ( usize_to_u64 ( consumed ) )
614617 . and_then ( |s| s. checked_sub ( len. into ( ) ) )
615618 . is_none ( ) ;
616619 if section_overflow {
@@ -1057,9 +1060,9 @@ fn delimited<'a, T>(
10571060 len : & mut u32 ,
10581061 f : impl FnOnce ( & mut BinaryReader < ' a > ) -> Result < T > ,
10591062) -> Result < T > {
1060- let start = reader. position ;
1063+ let start = reader. original_position ( ) ;
10611064 let ret = f ( reader) ?;
1062- * len = match ( reader. position - start)
1065+ * len = match ( reader. original_position ( ) - start)
10631066 . try_into ( )
10641067 . ok ( )
10651068 . and_then ( |i| len. checked_sub ( i) )
0 commit comments