{"record":{"id":"db46023298cd0146","repo":"mozilla/pdf.js","slug":"malformed-cmap-expected-string","errorCode":null,"errorMessage":"Malformed CMap: expected string.","messagePattern":"Malformed CMap: expected string\\.","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/cmap.js","lineNumber":452,"sourceCode":"  }\n\n  // eslint-disable-next-line getter-return\n  get isIdentityCMap() {\n    unreachable(\"should not access .isIdentityCMap\");\n  }\n}\n\nfunction strToInt(str) {\n  let a = 0;\n  for (let i = 0; i < str.length; i++) {\n    a = (a << 8) | str.charCodeAt(i);\n  }\n  return a >>> 0;\n}\n\nfunction expectString(obj) {\n  if (typeof obj !== \"string\") {\n    throw new FormatError(\"Malformed CMap: expected string.\");\n  }\n}\n\nfunction expectInt(obj) {\n  if (!Number.isInteger(obj)) {\n    throw new FormatError(\"Malformed CMap: expected int.\");\n  }\n}\n\nfunction parseBfChar(cMap, lexer) {\n  while (true) {\n    let obj = lexer.getObj();\n    if (obj === EOF) {\n      break;\n    }\n    if (isCmd(obj, \"endbfchar\")) {\n      return;\n    }","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/cmap.js#L434-L470","documentation":"Thrown by the expectString helper while parsing a CMap when the lexer yields an object that is not a JS string at a position where a hex/string token is mandatory (for example the source code points inside a beginbfchar block). It signals a structurally malformed CMap stream rather than a recoverable typo.","triggerScenarios":"Parsing a CMap stream where a token expected to be a hex string such as <0041> is instead a number, a Name, a Cmd, or EOF. Reached via parseBfChar / parseCidChar / similar parse* helpers that call expectString.","commonSituations":"Truncated ToUnicode stream; a PDF whose /ToUnicode entry points at a non-CMap stream; a broken CMap writer that emitted numbers where strings were required.","solutions":["Validate the PDF in an authoritative viewer and regenerate it if it fails.","Update pdf.js.","Catch FormatError around document load and skip ToUnicode for the offending font."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await loadingTask.promise;\n} catch (e) {\n  if (e instanceof FormatError && /expected string/.test(e.message)) {\n    console.warn('Malformed CMap (expected string), text selection may degrade', e);\n  } else throw e;\n}","preventionTips":["Validate PDFs from untrusted sources before rendering.","Keep pdf.js updated for more tolerant CMap parsing.","If you generate PDFs, validate the emitted ToUnicode streams parse cleanly.","Treat ToUnicode failures as non-fatal: rendering can continue without them."],"tags":["cmap","tounicode","font","parsing","pdf"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}