File tree Expand file tree Collapse file tree 4 files changed +23
-3
lines changed
Expand file tree Collapse file tree 4 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -681,18 +681,23 @@ class XRef {
681681 // When no trailer dictionary candidate exists, try picking the first
682682 // dictionary that contains a /Root entry (fixes issue18986.pdf).
683683 if ( ! trailerDicts . length ) {
684- for ( const [ num , entry ] of this . entries . entries ( ) ) {
685- if ( ! entry ) {
684+ // In case, this.entries is a sparse array we don't want to
685+ // iterate over empty entries so we use the `in` operator instead of
686+ // using for..of on entries() or a for with the array length.
687+ for ( const num in this . entries ) {
688+ if ( ! Object . hasOwn ( this . entries , num ) ) {
686689 continue ;
687690 }
688- const ref = Ref . get ( num , entry . gen ) ;
691+ const entry = this . entries [ num ] ;
692+ const ref = Ref . get ( parseInt ( num ) , entry . gen ) ;
689693 let obj ;
690694
691695 try {
692696 obj = this . fetch ( ref ) ;
693697 } catch {
694698 continue ;
695699 }
700+
696701 if ( obj instanceof BaseStream ) {
697702 obj = obj . dict ;
698703 }
Original file line number Diff line number Diff line change 741741! print_protection.pdf
742742! tracemonkey_with_annotations.pdf
743743! tracemonkey_with_editable_annotations.pdf
744+ ! bug1980958.pdf
Original file line number Diff line number Diff line change @@ -878,6 +878,20 @@ describe("api", function () {
878878
879879 await loadingTask . destroy ( ) ;
880880 } ) ;
881+
882+ it ( "Doesn't iterate over all empty slots in the xref entries (bug 1980958)" , async function ( ) {
883+ if ( isNodeJS ) {
884+ pending ( "Worker is not supported in Node.js." ) ;
885+ }
886+ const loadingTask = getDocument ( buildGetDocumentParams ( "bug1980958.pdf" ) ) ;
887+ const { promise, resolve } = Promise . withResolvers ( ) ;
888+ setTimeout ( ( ) => resolve ( null ) , 1000 ) ;
889+
890+ const pdfDocument = await Promise . race ( [ loadingTask . promise , promise ] ) ;
891+ expect ( pdfDocument ?. numPages ) . toEqual ( 1 ) ;
892+
893+ loadingTask . _worker . destroy ( ) ;
894+ } ) ;
881895 } ) ;
882896
883897 describe ( "PDFWorker" , function ( ) {
You can’t perform that action at this time.
0 commit comments