{"record":{"id":"a7bc99e93306a881","repo":"mozilla/pdf.js","slug":"extractpages-invalid-page-index","errorCode":null,"errorMessage":"extractPages: invalid page index.","messagePattern":"extractPages: invalid page index\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/editor/pdf_editor.js","lineNumber":991,"sourceCode":"   *  the annotations.\n   * @param {WorkerTask} task - The worker task to use for reporting progress\n   *  and cancellation.\n   * @return {Promise<void>}\n   */\n  async extractPages(\n    pageInfos,\n    annotationStorage,\n    primaryDocument,\n    handler,\n    task\n  ) {\n    this.#primaryDocument = primaryDocument;\n    pageInfos = this.#resolveInsertAfterIndices(pageInfos);\n    const promises = [];\n    let newIndex = 0;\n    const reservePageSlot = newPageIndex => {\n      if (!Number.isInteger(newPageIndex) || newPageIndex < 0) {\n        throw new Error(\"extractPages: invalid page index.\");\n      }\n      if (this.oldPages[newPageIndex] !== undefined) {\n        throw new Error(\"extractPages: overlapping pageIndices.\");\n      }\n      // Reserve the slot immediately because page/image collection can be\n      // async.\n      this.oldPages[newPageIndex] = null;\n    };\n    const allDocumentData = [];\n\n    if (annotationStorage) {\n      this.#newAnnotationsParams = {\n        handler,\n        task,\n        newAnnotationsByPage: getNewAnnotationsMap(annotationStorage),\n        imagesPromises: AnnotationFactory.generateImages(\n          annotationStorage.values(),\n          this.xrefWrapper,","sourceCodeStart":973,"sourceCodeEnd":1009,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/editor/pdf_editor.js#L973-L1009","documentation":"Plain Error thrown by reservePageSlot in extractPages when a computed or supplied newPageIndex is not an integer or is negative. Page slots must be non-negative integers because they index into the oldPages array of the new document.","triggerScenarios":"Passing pageIndices containing a non-integer (NaN, float, string-coerced) or a negative value; an internal arithmetic bug producing NaN; an insertAfter value of -2 or lower combined with offset math that goes negative.","commonSituations":"Caller builds pageIndices from user input without validation; floating-point off-by-one from index calculations; off-by-one in insertAfter resolution.","solutions":["Validate every pageIndex with Number.isInteger(x) && x >= 0 before building pageInfos.","Sanitize user-supplied insertAfter values to integers >= -1.","Add a unit test that feeds edge-case pageIndices to your pageInfo builder."],"exampleFix":"// before\npageInfos = [{ document, pageIndices: [0.5, -1, 2] }];\n\n// after\nconst idx = [0.5, -1, 2].filter(x => Number.isInteger(x) && x >= 0);\nif (idx.length !== 3) throw new Error('pageIndices must be non-negative integers');\npageInfos = [{ document, pageIndices: idx }];","handlingStrategy":"validation","validationCode":"function assertValidPageIndices(indices) {\n  for (const idx of indices) {\n    if (!Number.isInteger(idx) || idx < 0) {\n      throw new Error(`Invalid pageIndex: ${idx}`);\n    }\n  }\n}","typeGuard":"function isValidPageIndex(x) {\n  return Number.isInteger(x) && x >= 0;\n}","tryCatchPattern":"try {\n  await editor.extractPages(pageInfos, ...);\n} catch (e) {\n  if (e.message === 'extractPages: invalid page index.') {\n    pageInfos.forEach(p => p.pageIndices?.forEach(i => console.warn('bad index', i)));\n    return;\n  }\n  throw e;\n}","preventionTips":["Always validate pageIndices with Number.isInteger and >= 0 before building pageInfos.","Sanitize user-supplied insertAfter to integers >= -1.","Use TypeScript types that reject non-integer pageIndices at compile time."],"tags":["editor","extract-pages","validation","page-index"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}