{"record":{"id":"935f09588bdc96ca","repo":"parallax/jsPDF","slug":"width-height-invalid","errorCode":null,"errorMessage":"Width/Height invalid.","messagePattern":"Width/Height invalid\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/libs/omggif.js","lineNumber":42,"sourceCode":" *\n * omggif is a JavaScript implementation of a GIF 89a encoder and decoder,\n * including animation and compression.  It does not rely on any specific\n * underlying system, so should run in the browser, Node, or Plask.\n */\n\n\"use strict\";\n\nimport { console } from \"./console.js\";\n\nfunction GifWriter(buf, width, height, gopts) {\n  var p = 0;\n\n  var gopts = gopts === undefined ? {} : gopts;\n  var loop_count = gopts.loop === undefined ? null : gopts.loop;\n  var global_palette = gopts.palette === undefined ? null : gopts.palette;\n\n  if (width <= 0 || height <= 0 || width > 65535 || height > 65535)\n    throw new Error(\"Width/Height invalid.\");\n\n  function check_palette_and_num_colors(palette) {\n    var num_colors = palette.length;\n    if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors - 1)) {\n      throw new Error(\n        \"Invalid code/color length, must be power of 2 and 2 .. 256.\"\n      );\n    }\n    return num_colors;\n  }\n\n  // - Header.\n  buf[p++] = 0x47;\n  buf[p++] = 0x49;\n  buf[p++] = 0x46; // GIF\n  buf[p++] = 0x38;\n  buf[p++] = 0x39;\n  buf[p++] = 0x61; // 89a","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/libs/omggif.js#L24-L60","documentation":"Thrown by GifWriter when the canvas width or height is <= 0 or exceeds 65535. GIF stores dimensions as unsigned 16-bit fields, so any value outside 1..65535 for either dimension is invalid before the Logical Screen Descriptor is written.","triggerScenarios":"Calling new GifWriter(buf, width, height, gopts) with width/height of 0, negative, NaN (which fails the <= 0 test), or > 65535. Computing dimensions from a zero-size source canvas; passing undefined which becomes NaN.","commonSituations":"Generating a GIF from a canvas whose width/height were never set; off-by-one producing 0; upscaling beyond the GIF spec limit; floating-point dimension not floored to an integer.","solutions":["Clamp width/height to integers in 1..65535 before constructing GifWriter.","Ensure the source canvas/image has non-zero natural dimensions.","Floor any fractional dimension: Math.max(1, Math.floor(w))."],"exampleFix":"// before\nvar gw = new GifWriter(buf, canvas.width, canvas.height, {});\n// after\nvar w = Math.max(1, Math.min(65535, Math.floor(canvas.width)));\nvar h = Math.max(1, Math.min(65535, Math.floor(canvas.height)));\nvar gw = new GifWriter(buf, w, h, {});","handlingStrategy":"validation","validationCode":"function validGifDim(d){ d = Math.floor(d); return d >= 1 && d <= 65535 ? d : null; }\nvar w = validGifDim(width), h = validGifDim(height);\nif (w && h) new GifWriter(buf, w, h, gopts);","typeGuard":"function isGifDimension(v){ return Number.isInteger(v) && v >= 1 && v <= 65535; }","tryCatchPattern":"try { new GifWriter(buf, w, h, gopts); } catch (e) { if (/Width\\/Height invalid/.test(e.message)) { /* fix dims */ } else throw e; }","preventionTips":["Floor fractional canvas dimensions","Reject zero-size source canvases early"],"tags":["gif","omggif","image","validation","dimensions"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}