File tree Expand file tree Collapse file tree 5 files changed +23
-3
lines changed Expand file tree Collapse file tree 5 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -315,7 +315,7 @@ class Dict {
315
315
316
316
class Ref {
317
317
constructor ( num , gen ) {
318
- this . num = num ;
318
+ this . num = parseInt ( num ) ;
319
319
this . gen = gen ;
320
320
}
321
321
Original file line number Diff line number Diff line change @@ -681,10 +681,14 @@ class XRef {
681
681
// When no trailer dictionary candidate exists, try picking the first
682
682
// dictionary that contains a /Root entry (fixes issue18986.pdf).
683
683
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 ) ) {
686
689
continue ;
687
690
}
691
+ const entry = this . entries [ num ] ;
688
692
const ref = Ref . get ( num , entry . gen ) ;
689
693
let obj ;
690
694
@@ -693,6 +697,7 @@ class XRef {
693
697
} catch {
694
698
continue ;
695
699
}
700
+
696
701
if ( obj instanceof BaseStream ) {
697
702
obj = obj . dict ;
698
703
}
Original file line number Diff line number Diff line change 740
740
! dates_save.pdf
741
741
! print_protection.pdf
742
742
! tracemonkey_with_annotations.pdf
743
+ ! bug1980958.pdf
Original file line number Diff line number Diff line change @@ -878,6 +878,20 @@ describe("api", function () {
878
878
879
879
await loadingTask . destroy ( ) ;
880
880
} ) ;
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
+ } ) ;
881
895
} ) ;
882
896
883
897
describe ( "PDFWorker" , function ( ) {
You can’t perform that action at this time.
0 commit comments