{"record":{"id":"f54c899df9516db6","repo":"mozilla/pdf.js","slug":"invalid-rotation","errorCode":null,"errorMessage":"Invalid rotation","messagePattern":"Invalid rotation","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/core_utils.js","lineNumber":736,"sourceCode":"    date.getUTCDate().toString().padStart(2, \"0\"),\n    date.getUTCHours().toString().padStart(2, \"0\"),\n    date.getUTCMinutes().toString().padStart(2, \"0\"),\n    date.getUTCSeconds().toString().padStart(2, \"0\"),\n  ];\n\n  return buffer.join(\"\");\n}\n\nfunction getRotationMatrix(rotation, width, height) {\n  switch (rotation) {\n    case 90:\n      return [0, 1, -1, 0, width, 0];\n    case 180:\n      return [-1, 0, 0, -1, width, height];\n    case 270:\n      return [0, -1, 1, 0, 0, height];\n    default:\n      throw new Error(\"Invalid rotation\");\n  }\n}\n\n/**\n * Get the number of bytes to use to represent the given positive integer.\n * If n is zero, the function returns 0 which means that we don't need to waste\n * a byte to represent it.\n * @param {number} x - a positive integer.\n * @returns {number}\n */\nfunction getSizeInBytes(x) {\n  // n bits are required for numbers up to 2^n - 1.\n  // So for a number x, we need ceil(log2(1 + x)) bits.\n  return Math.ceil(Math.ceil(Math.log2(1 + x)) / 8);\n}\n\nexport {\n  arrayBuffersToBytes,","sourceCodeStart":718,"sourceCodeEnd":754,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/core_utils.js#L718-L754","documentation":"Thrown by getRotationMatrix for any rotation value that is not exactly 90, 180, or 270 - the switch's default case. Notably, rotation = 0 also throws, because 0 (the identity) is intentionally not handled here; callers are expected to skip the call for 0 and pass the identity matrix themselves.","triggerScenarios":"Calling getRotationMatrix(0, w, h) (forgetting that 0 is unsupported), or passing a non-multiple of 90 (45, 360, -90) or an unnormalized raw /Rotate value read straight from a PDF page dictionary.","commonSituations":"Reading /Rotate from a PDF page dict without normalizing; passing user-supplied rotation through without validation; forgetting the 0-identity special case that every internal caller guards with `rotation !== 0 ? getRotationMatrix(...) : undefined`.","solutions":["Normalize before calling: r = ((rotation % 360) + 360) % 360.","Skip the call when r === 0 and use the identity matrix [1, 0, 0, 1, 0, 0].","Reject or clamp non-multiple-of-90 values upstream before they reach this function."],"exampleFix":"// before\nconst m = getRotationMatrix(rotation, w, h);\n\n// after\nconst r = ((rotation % 360) + 360) % 360;\nconst m =\n  r === 0\n    ? [1, 0, 0, 1, 0, 0]\n    : getRotationMatrix(r, w, h);","handlingStrategy":"validation","validationCode":"// Normalize rotation and skip the identity case before calling getRotationMatrix\nfunction safeRotationMatrix(rotation, w, h) {\n  const r = ((rotation % 360) + 360) % 360;\n  if (r === 0) return [1, 0, 0, 1, 0, 0];\n  if (r % 90 !== 0) throw new Error(`Unsupported rotation: ${rotation}`);\n  return getRotationMatrix(r, w, h);\n}","typeGuard":"function isSupportedRotation(rotation) {\n  const r = ((Number(rotation) % 360) + 360) % 360;\n  return r === 0 || r === 90 || r === 180 || r === 270;\n}","tryCatchPattern":null,"preventionTips":["Always normalize rotation with ((rotation % 360) + 360) % 360 before use.","Handle rotation === 0 as the identity; never pass it to getRotationMatrix.","Reject non-multiple-of-90 rotation values upstream from your UI/API.","Mirror the internal callers' guard: `rotation !== 0 ? getRotationMatrix(...) : undefined`."],"tags":["rotation","transform","api-misuse","pdf"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}