{"record":{"id":"afddf61e7481c0f4","repo":"mozilla/pdf.js","slug":"count-must-be-a-positive-integer","errorCode":null,"errorMessage":"Count must be a (positive) integer.","messagePattern":"Count must be a \\(positive\\) integer\\.","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/catalog.js","lineNumber":1659,"sourceCode":"        if (!(kid instanceof Ref)) {\n          throw new FormatError(\"Kid must be a reference.\");\n        }\n        if (isRefsEqual(kid, ref)) {\n          found = true;\n          break;\n        }\n        kidPromises.push(\n          xref.fetchAsync(kid).then(obj => {\n            if (!(obj instanceof Dict)) {\n              throw new FormatError(\"Kid node must be a dictionary.\");\n            }\n            if (obj.has(\"Count\")) {\n              const count = obj.get(\"Count\");\n              if (Number.isInteger(count) && count >= 0) {\n                total += count;\n                return;\n              }\n              throw new FormatError(\"Count must be a (positive) integer.\");\n            }\n            // Page leaf node.\n            total++;\n          })\n        );\n      }\n      if (!found) {\n        throw new FormatError(\"Kid reference not found in parent's kids.\");\n      }\n      await Promise.all(kidPromises);\n      ref = parentRef;\n    }\n\n    this.pageIndexCache.put(pageRef, total);\n    return total;\n  }\n\n  get baseUrl() {","sourceCodeStart":1641,"sourceCodeEnd":1677,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/catalog.js#L1641-L1677","documentation":"Thrown during getPageIndex kid counting when an intermediate /Pages node has a /Count entry that is not a non-negative integer. Intermediate nodes use /Count to declare how many leaf pages lie beneath them; an invalid count makes index computation impossible.","triggerScenarios":"A kid node (resolved to a Dict) has a /Count entry that is a float, negative number, string, or other non-integer. The guard is `Number.isInteger(count) && count >= 0` failing while `obj.has('Count')` is true.","commonSituations":"Generators that write /Count as a real number or leave a placeholder value; corrupt files where /Count bytes were altered; spec-violating producers that omit or miscompute /Count on intermediate nodes.","solutions":["Catch the FormatError from getPageIndex and fall back to a linear page scan.","Rebuild the PDF with Ghostscript/qpdf so intermediate /Count values are correct integers.","Regenerate from source with a compliant PDF library."],"exampleFix":"// before\nconst pageIndex = await pdfDocument.getPageIndex(pageRef);\n\n// after\nlet pageIndex = -1;\ntry {\n  pageIndex = await pdfDocument.getPageIndex(pageRef);\n} catch (e) {\n  for (let i = 0; i < pdfDocument.numPages; i++) {\n    const page = await pdfDocument.getPage(i);\n    if (page.ref && page.ref.num === pageRef.num) { pageIndex = i; break; }\n  }\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try {\n  idx = await pdfDocument.getPageIndex(ref);\n} catch (e) {\n  for (let i = 0; i < pdfDocument.numPages; i++) {\n    const p = await pdfDocument.getPage(i);\n    if (p.ref && p.ref.num === ref.num) { idx = i; break; }\n  }\n}","preventionTips":["When building page trees, compute /Count as a non-negative integer leaf count.","Repair PDFs with Ghostscript to recompute /Count values correctly.","Treat getPageIndex as fallible for third-party PDFs and provide a fallback."],"tags":["pdf","catalog","page-tree","corrupt-pdf","count"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}