{"record":{"id":"4172a51de1b32c73","repo":"gchq/CyberChef","slug":"block-out-of-range","errorCode":null,"errorMessage":"Block out of range.","messagePattern":"Block out of range\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/IP.mjs","lineNumber":312,"sourceCode":"    result += numBlocks[0] << 24;\n    result += numBlocks[1] << 16;\n    result += numBlocks[2] << 8;\n    result += numBlocks[3];\n\n    return result;\n\n    /**\n     * Converts a list of 4 numeric strings in the range 0-255 to a list of numbers.\n     */\n    function parseBlocks(blocks) {\n        if (blocks.length !== 4)\n            throw new OperationError(\"More than 4 blocks.\");\n\n        const numBlocks = [];\n        for (let i = 0; i < 4; i++) {\n            numBlocks[i] = parseInt(blocks[i], 10);\n            if (numBlocks[i] < 0 || numBlocks[i] > 255)\n                throw new OperationError(\"Block out of range.\");\n        }\n        return numBlocks;\n    }\n}\n\n/**\n * Converts an IPv4 address from numerical format to string format.\n *\n * @param {number} ipInt\n * @returns {string}\n *\n * @example\n * // returns \"10.10.0.0\"\n * ipv4ToStr(168427520);\n */\nexport function ipv4ToStr(ipInt) {\n    const blockA = (ipInt >> 24) & 255,\n        blockB = (ipInt >> 16) & 255,","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/IP.mjs#L294-L330","documentation":"Thrown by strToIpv4's parseBlocks when any octet, after parseInt base 10, falls outside 0-255. Note a quirk: parseInt of non-numeric text yields NaN, and NaN is neither <0 nor >255, so non-numeric octets slip through this guard (a latent bug). The check reliably catches out-of-range numeric octets like 256. OperationError.","triggerScenarios":"Calling strToIpv4('1.2.3.256'), '1.2.3.-1', or '10.0.0.300'. Non-numeric junk in an octet may bypass this and produce NaN downstream instead.","commonSituations":"Typo producing an octet >255; negative value; leading-zero or decimal confusion; off-by-one in generated IPs.","solutions":["Validate each octet is a base-10 integer in [0,255] before calling strToIpv4 (also reject NaN/non-numeric).","Use an IPv4 regex that bounds each octet to 0-255.","Sanitise generated IP lists before parsing."],"exampleFix":"// before\nstrToIpv4(\"10.0.0.256\");\n\n// after\nstrToIpv4(\"10.0.0.255\");","handlingStrategy":"validation","validationCode":"function assertIpv4Octets(ipStr) {\n  const blocks = ipStr.split(\".\");\n  if (blocks.length !== 4) throw new Error(\"Need exactly 4 octets\");\n  for (const b of blocks) {\n    const n = Number.parseInt(b, 10);\n    if (!Number.isInteger(n) || n < 0 || n > 255) {\n      throw new Error(`Octet '${b}' out of range 0-255`);\n    }\n  }\n}\nassertIpv4Octets(ipStr);\nstrToIpv4(ipStr);","typeGuard":"const isIpv4InRange = ipStr =>\n  ipStr.split(\".\").every(b => { const n = Number.parseInt(b, 10); return Number.isInteger(n) && n >= 0 && n <= 255; });","tryCatchPattern":"try {\n  strToIpv4(ipStr);\n} catch (err) {\n  if (err instanceof OperationError && /Block out of range/.test(err.message)) {\n    // an octet exceeded 255; correct the input\n  } else throw err;\n}","preventionTips":["Use Number.parseInt and reject NaN explicitly (the lib's guard lets NaN through).","Bound each octet to 0-255 with a regex or explicit check.","Sanitise generated IP lists before parsing."],"tags":["ip","ipv4","validation","range"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}