gchq/CyberChef · error · OperationError

More than 4 blocks.

Error message

More than 4 blocks.

What it means

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.

Source

Thrown at src/core/lib/IP.mjs:306

 */
export function strToIpv4(ipStr) {
    const blocks = ipStr.split("."),
        numBlocks = parseBlocks(blocks);
    let result = 0;

    result += numBlocks[0] << 24;
    result += numBlocks[1] << 16;
    result += numBlocks[2] << 8;
    result += numBlocks[3];

    return result;

    /**
     * Converts a list of 4 numeric strings in the range 0-255 to a list of numbers.
     */
    function parseBlocks(blocks) {
        if (blocks.length !== 4)
            throw new OperationError("More than 4 blocks.");

        const numBlocks = [];
        for (let i = 0; i < 4; i++) {
            numBlocks[i] = parseInt(blocks[i], 10);
            if (numBlocks[i] < 0 || numBlocks[i] > 255)
                throw new OperationError("Block out of range.");
        }
        return numBlocks;
    }
}

/**
 * Converts an IPv4 address from numerical format to string format.
 *
 * @param {number} ipInt
 * @returns {string}
 *
 * @example

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the string has exactly four dot-separated octets.
  2. Validate with an IPv4 regex before calling strToIpv4.
  3. Route IPv6 or non-IP input to the correct parser.

Example fix

// before
strToIpv4("10.0.0");

// after
strToIpv4("10.0.0.0");
Defensive patterns

Strategy: validation

Validate before calling

function assertIpv4String(ipStr) {
  const blocks = ipStr.split(".");
  if (blocks.length !== 4) {
    throw new Error(`IPv4 must have exactly 4 dot-separated octets, got ${blocks.length}`);
  }
}
assertIpv4String(ipStr);
strToIpv4(ipStr);

Type guard

const isIpv4Shape = ipStr => /^\d{1,3}(\.\d{1,3}){3}$/.test(ipStr);

Try / catch

try {
  strToIpv4(ipStr);
} catch (err) {
  if (err instanceof OperationError && /More than 4 blocks/.test(err.message)) {
    // malformed IPv4 (also fires for fewer than 4); correct the input
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: Typo missing an octet; trailing/leading dots; an IPv6 address routed to the IPv4 parser; a hostname like 'localhost'.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/45088e3509ba2762. Report an issue: GitHub.