@@ -33,6 +33,7 @@ import {
3333import { Lexer , Parser } from "./parser.js" ;
3434import {
3535 MissingDataException ,
36+ ParserEOFException ,
3637 XRefEntryException ,
3738 XRefParseException ,
3839} from "./core_utils.js" ;
@@ -453,15 +454,38 @@ class XRef {
453454 } else if ( ( m = objRegExp . exec ( token ) ) ) {
454455 const num = m [ 1 ] | 0 ,
455456 gen = m [ 2 ] | 0 ;
456- if ( ! this . entries [ num ] || this . entries [ num ] . gen === gen ) {
457+
458+ let contentLength ,
459+ startPos = position + token . length ,
460+ updateEntries = false ;
461+ if ( ! this . entries [ num ] ) {
462+ updateEntries = true ;
463+ } else if ( this . entries [ num ] . gen === gen ) {
464+ // Before overwriting an existing entry, ensure that the new one won't
465+ // cause *immediate* errors when it's accessed (fixes issue13783.pdf).
466+ try {
467+ const parser = new Parser ( {
468+ lexer : new Lexer ( stream . makeSubStream ( startPos ) ) ,
469+ } ) ;
470+ parser . getObj ( ) ;
471+ updateEntries = true ;
472+ } catch ( ex ) {
473+ if ( ex instanceof ParserEOFException ) {
474+ warn ( `indexObjects -- checking object (${ token } ): "${ ex } ".` ) ;
475+ } else {
476+ // The error may come from the `Parser`-instance being initialized
477+ // without an `XRef`-instance (we don't have a usable one yet).
478+ updateEntries = true ;
479+ }
480+ }
481+ }
482+ if ( updateEntries ) {
457483 this . entries [ num ] = {
458484 offset : position - stream . start ,
459485 gen,
460486 uncompressed : true ,
461487 } ;
462488 }
463- let contentLength ,
464- startPos = position + token . length ;
465489
466490 // Find the next "obj" string, rather than "endobj", to ensure that
467491 // we won't skip over a new 'obj' operator in corrupt files where
0 commit comments