{"record":{"id":"45088e3509ba2762","repo":"gchq/CyberChef","slug":"more-than-4-blocks","errorCode":null,"errorMessage":"More than 4 blocks.","messagePattern":"More than 4 blocks\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/IP.mjs","lineNumber":306,"sourceCode":" */\nexport function strToIpv4(ipStr) {\n    const blocks = ipStr.split(\".\"),\n        numBlocks = parseBlocks(blocks);\n    let result = 0;\n\n    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","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/IP.mjs#L288-L324","documentation":"Thrown by the inner parseBlocks of strToIpv4 when `ipStr.split(\".\")` does not yield exactly 4 elements. The message says 'More than 4 blocks.' but the guard (`blocks.length !== 4`) also fires for fewer than 4 (e.g. a bare '10.0.0'). OperationError.","triggerScenarios":"Calling strToIpv4('1.2.3') (3 blocks), '1.2.3.4.5' (5 blocks), '' (1 block), '10.0.0.' (trailing dot produces an empty 5th), or an IPv6/hostname string.","commonSituations":"Typo missing an octet; trailing/leading dots; an IPv6 address routed to the IPv4 parser; a hostname like 'localhost'.","solutions":["Ensure the string has exactly four dot-separated octets.","Validate with an IPv4 regex before calling strToIpv4.","Route IPv6 or non-IP input to the correct parser."],"exampleFix":"// before\nstrToIpv4(\"10.0.0\");\n\n// after\nstrToIpv4(\"10.0.0.0\");","handlingStrategy":"validation","validationCode":"function assertIpv4String(ipStr) {\n  const blocks = ipStr.split(\".\");\n  if (blocks.length !== 4) {\n    throw new Error(`IPv4 must have exactly 4 dot-separated octets, got ${blocks.length}`);\n  }\n}\nassertIpv4String(ipStr);\nstrToIpv4(ipStr);","typeGuard":"const isIpv4Shape = ipStr => /^\\d{1,3}(\\.\\d{1,3}){3}$/.test(ipStr);","tryCatchPattern":"try {\n  strToIpv4(ipStr);\n} catch (err) {\n  if (err instanceof OperationError && /More than 4 blocks/.test(err.message)) {\n    // malformed IPv4 (also fires for fewer than 4); correct the input\n  } else throw err;\n}","preventionTips":["Validate with an IPv4 regex before parsing.","Note the message says 'More than 4' but the check also rejects fewer than 4.","Route IPv6/hostname input to the correct parser."],"tags":["ip","ipv4","validation","parsing"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}