{"record":{"id":"16718d69c20568a2","repo":"mozilla/pdf.js","slug":"createimage-invalid-bitmap-dimensions-width-x","errorCode":null,"errorMessage":"createImage: invalid bitmap dimensions ${width}x${height}","messagePattern":"createImage: invalid bitmap dimensions (.+?)x(.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/editor/pdf_images.js","lineNumber":170,"sourceCode":"  return createRawImage(compressed, dict);\n}\n\nasync function createImage(bitmap, xref, { closeBitmap = false } = {}) {\n  // TODO: when printing, we could have a specific internal colorspace\n  // (e.g. something like DeviceRGBA) in order avoid any conversion (i.e. no\n  // jpeg, no rgba to rgb conversion, etc...)\n\n  const { width, height } = bitmap;\n  if (\n    !Number.isInteger(width) ||\n    !Number.isInteger(height) ||\n    width <= 0 ||\n    height <= 0\n  ) {\n    if (closeBitmap) {\n      bitmap.close?.();\n    }\n    throw new Error(\n      `createImage: invalid bitmap dimensions ${width}x${height}`\n    );\n  }\n  const canvas = new OffscreenCanvas(width, height);\n  const ctx = canvas.getContext(\"2d\", {\n    alpha: true,\n    willReadFrequently: true,\n  });\n\n  let data;\n  try {\n    ctx.drawImage(bitmap, 0, 0);\n    data = ctx.getImageData(0, 0, width, height).data;\n  } finally {\n    if (closeBitmap) {\n      bitmap.close?.();\n    }\n  }","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/editor/pdf_images.js#L152-L188","documentation":"Plain Error thrown by createImage when the supplied bitmap's width or height is not a positive integer (zero, negative, NaN, Infinity, or fractional). The function immediately calls new OffscreenCanvas(width, height), which requires positive integer dimensions, so the guard prevents a cryptic TypeError from the platform.","triggerScenarios":"Passing a degenerate ImageBitmap (e.g., from createImageBitmap of an empty/corrupt source); a canvas whose backing store was lost; passing a custom bitmap-like object with NaN/Infinity dimensions; a closed ImageBitmap whose width/height read as 0.","commonSituations":"Decoding a corrupt image file via createImageBitmap then handing the result to createImage; memory pressure where the bitmap failed to allocate; passing width/height from user input without validation.","solutions":["Validate bitmap.width and bitmap.height with Number.isInteger and > 0 before calling createImage.","Ensure the upstream createImageBitmap / decode succeeded (check the bitmap is not closed and has positive dimensions).","Catch the error and skip or substitute a placeholder image."],"exampleFix":"// before\nconst pdfImage = await createImage(bitmap, xref, { closeBitmap: true });\n\n// after\nif (!Number.isInteger(bitmap.width) || !Number.isInteger(bitmap.height) || bitmap.width <= 0 || bitmap.height <= 0) {\n  throw new Error(`Bitmap has invalid dimensions ${bitmap.width}x${bitmap.height}; source image may be corrupt.`);\n}\nconst pdfImage = await createImage(bitmap, xref, { closeBitmap: true });","handlingStrategy":"validation","validationCode":"function isValidBitmap(b) {\n  return b && Number.isInteger(b.width) && Number.isInteger(b.height) && b.width > 0 && b.height > 0;\n}\nif (!isValidBitmap(bitmap)) {\n  throw new Error(`Bitmap dimensions invalid: ${bitmap?.width}x${bitmap?.height}`);\n}","typeGuard":"function isValidBitmap(b) {\n  return b != null &&\n    Number.isInteger(b.width) && b.width > 0 &&\n    Number.isInteger(b.height) && b.height > 0 &&\n    typeof b.close === 'function';\n}","tryCatchPattern":"try {\n  return await createImage(bitmap, xref, { closeBitmap: true });\n} catch (e) {\n  if (/invalid bitmap dimensions/.test(e.message)) {\n    bitmap.close?.();\n    return fallbackPlaceholderImage(xref);\n  }\n  throw e;\n}","preventionTips":["Validate bitmap dimensions immediately after createImageBitmap resolves.","Handle upstream decode failures before forwarding bitmaps to createImage.","Cap source image dimensions at upload to avoid degenerate bitmaps."],"tags":["image","editor","validation","bitmap","createimage"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}