{"record":{"id":"509cc9e804f5ff6a","repo":"microsoft/playwright","slug":"invalid-output-dimensions","errorCode":null,"errorMessage":"Invalid output dimensions","messagePattern":"Invalid output dimensions","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/isomorphic/imageUtils.ts","lineNumber":54,"sourceCode":"        buffer[to + 3] = 0;\n      }\n    }\n  }\n  return { data: Buffer.from(buffer), width: size.width, height: size.height };\n}\n\nexport function scaleImageToSize(image: ImageData, size: { width: number; height: number }): ImageData {\n  const { data: src, width: w1, height: h1 } = image;\n  const w2 = Math.max(1, Math.floor(size.width));\n  const h2 = Math.max(1, Math.floor(size.height));\n\n  if (w1 === w2 && h1 === h2)\n    return image;\n\n  if (w1 <= 0 || h1 <= 0)\n    throw new Error('Invalid input image');\n  if (size.width <= 0 || size.height <= 0 || !isFinite(size.width) || !isFinite(size.height))\n    throw new Error('Invalid output dimensions');\n\n  const clamp = (v: number, lo: number, hi: number) => (v < lo ? lo : v > hi ? hi : v);\n\n  // Catmull–Rom weights\n  const weights = (t: number, o: Float32Array) => {\n    const t2 = t * t;\n    const t3 = t2 * t;\n    o[0] = -0.5 * t + 1.0 * t2 - 0.5 * t3;\n    o[1] = 1.0 - 2.5 * t2 + 1.5 * t3;\n    o[2] = 0.5 * t + 2.0 * t2 - 1.5 * t3;\n    o[3] = -0.5 * t2 + 0.5 * t3;\n  };\n\n  const srcRowStride = w1 * 4;\n  const dstRowStride = w2 * 4;\n\n  // Precompute X: indices, weights, and byte offsets (idx*4)\n  const xOff = new Int32Array(w2 * 4); // byte offsets = xIdx*4","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/isomorphic/imageUtils.ts#L36-L72","documentation":"scaleImageToSize throws 'Invalid output dimensions' when the requested target width or height is <= 0, NaN, or +/- Infinity. The scaler needs a finite positive target to allocate the output buffer and compute sampling weights.","triggerScenarios":"Calling scaleImageToSize with a target size where size.width or size.height is 0, negative, NaN, or Infinity. Common when the target size is derived from a computation that can yield NaN (e.g. division by zero) or from an unset/optional field defaulting to 0.","commonSituations":"Computing a thumbnail size from an aspect ratio that produced NaN/Infinity; reading target dimensions from config that was not populated; clamping logic that collapsed a dimension to 0; passing window innerWidth/Height before layout (0).","solutions":["Validate Number.isFinite(size.width) && Number.isFinite(size.height) && size.width > 0 && size.height > 0 before calling.","Clamp targets to a minimum of 1 with Math.max(1, Math.floor(x)) and ensure the source of the number cannot be NaN.","Default optional config fields to concrete positive values rather than 0/undefined."],"exampleFix":"// before\nconst out = scaleImageToSize(img, { width: ratio, height: 0 }); // ratio may be NaN\n\n// after\nconst w = Number.isFinite(ratio) && ratio > 0 ? Math.floor(ratio) : 1;\nconst out = scaleImageToSize(img, { width: w, height: 64 });","handlingStrategy":"validation","validationCode":"function isValidTargetSize(s: {width:number;height:number}): boolean {\n  return Number.isFinite(s.width) && Number.isFinite(s.height) && s.width > 0 && s.height > 0;\n}","typeGuard":"function isPositiveSize(s: unknown): s is { width: number; height: number } {\n  return !!s && typeof s === 'object'\n    && Number.isFinite((s as any).width) && (s as any).width > 0\n    && Number.isFinite((s as any).height) && (s as any).height > 0;\n}","tryCatchPattern":"try { return scaleImageToSize(img, size); }\ncatch (e) { if (e.message === 'Invalid output dimensions') { size = { width: 1, height: 1 }; return scaleImageToSize(img, size); } throw e; }","preventionTips":["Clamp computed target dimensions to a minimum of 1 with Math.max(1, Math.floor(x)).","Guard against NaN/Infinity when deriving sizes from ratios.","Default optional config fields to concrete positive values."],"tags":["image","scaling","validation","dimensions","nan"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}