{"record":{"id":"482738d1feb6dadb","repo":"mozilla/pdf.js","slug":"binarycmapreader-process-invalid-datasize","errorCode":null,"errorMessage":"BinaryCMapReader.process: Invalid dataSize.","messagePattern":"BinaryCMapReader\\.process: Invalid dataSize\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/binary_cmap.js","lineNumber":175,"sourceCode":"    while ((b = stream.getByte()) >= 0) {\n      const type = b >> 5;\n      if (type === 7) {\n        // metadata, e.g. comment or usecmap\n        switch (b & 0x1f) {\n          case 0:\n            stream.readString(); // skipping comment\n            break;\n          case 1:\n            useCMap = stream.readString();\n            break;\n        }\n        continue;\n      }\n      const sequence = !!(b & 0x10);\n      const dataSize = b & 15;\n\n      if (dataSize + 1 > MAX_NUM_SIZE) {\n        throw new Error(\"BinaryCMapReader.process: Invalid dataSize.\");\n      }\n\n      const ucs2DataSize = 1;\n      const subitemsCount = stream.readNumber();\n      switch (type) {\n        case 0: // codespacerange\n          stream.readHex(start, dataSize);\n          stream.readHexNumber(end, dataSize);\n          addHex(end, start, dataSize);\n          cMap.addCodespaceRange(\n            dataSize + 1,\n            hexToInt(start, dataSize),\n            hexToInt(end, dataSize)\n          );\n          for (let i = 1; i < subitemsCount; i++) {\n            incHex(end, dataSize);\n            stream.readHexNumber(start, dataSize);\n            addHex(start, end, dataSize);","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/binary_cmap.js#L157-L193","documentation":"Thrown by BinaryCMapReader.process() (binary_cmap.js:175) when the low nibble of a record header byte yields dataSize such that dataSize + 1 exceeds MAX_NUM_SIZE (16). dataSize sizes the Uint8Array work buffers (start/end/char/charCode/tmp, all length 16), so a larger value would overflow them. The record is structurally invalid.","triggerScenarios":"While iterating records, b & 15 (dataSize) produces a value >= 16. Occurs when a .bcmap file is corrupt, when a non-bcmap byte sequence is fed to BinaryCMapReader, or when the record stream is misaligned because an earlier record was mis-parsed (e.g., from a preceding EOF/short read).","commonSituations":"Corrupt or hand-modified .bcmap resource; wrong file served at the CMap URL (e.g., a text CMap or HTML body parsed as binary); version skew between the bcmap writer and reader; bit-rot/disk corruption of the cached CMap.","solutions":["Replace the CMap resource at cMapUrl with a known-good copy from the matching PDF.js release.","Confirm the served file is actually a binary packed CMap (starts with the expected bcmap header byte) and not a text CMap or error page.","Re-derive/re-download the CMap set from the official source to eliminate bit-rot.","Match the PDF.js build version to the CMap distribution version."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Guard the CMap source: ensure the URL resolves a genuine binary bcmap.\nasync function assertBcMapOk(url) {\n  const res = await fetch(url);\n  if (!res.ok) throw new Error(`CMap fetch failed: ${res.status}`);\n  const buf = new Uint8Array(await res.arrayBuffer());\n  if (buf.length < 2) throw new Error('bcmap too short / corrupt');\n  return buf;\n}\n// Then rely on PDF.js by pointing cMapUrl at the validated directory.","typeGuard":null,"tryCatchPattern":"try {\n  await page.render({ canvasContext, viewport }).promise;\n} catch (err) {\n  if (/Invalid dataSize/i.test(err?.message)) {\n    console.warn('Corrupt binary CMap; replace the cmaps directory', err);\n  } else throw err;\n}","preventionTips":["Copy the entire cmaps folder from the matching pdfjs-dist release; never edit individual .bcmap files.","Do not serve text CMaps or HTML error pages at the cMapUrl.","Re-download CMaps if you suspect bit-rot or partial copy.","Keep pdfjs-dist and CMaps version-aligned."],"tags":["cmap","cjk-fonts","binary-cmap","corrupt-resource","validation"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}