gchq/CyberChef · error · OperationError

Width must be a positive integer

Error message

Width must be a positive integer

What it means

To Hexdump requires the line width (length) argument to be a positive integer. The guard `length < 1 || Math.round(length) !== length` rejects zero, negatives, and non-integer values, because the hexdump is laid out in fixed-width rows.

Source

Thrown at src/core/operations/ToHexdump.mjs:67

                "name": "UNIX format",
                "type": "boolean",
                "value": false
            }
        ];
    }

    /**
     * @param {ArrayBuffer} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const data = new Uint8Array(input);
        const [length, upperCase, includeFinalLength, unixFormat] = args;
        const padding = 2;

        if (length < 1 || Math.round(length) !== length)
            throw new OperationError("Width must be a positive integer");

        if (length > MAX_WIDTH)
            throw new OperationError(`Width must be no more than ${MAX_WIDTH}`);

        const lines = [];
        for (let i = 0; i < data.length; i += length) {
            let lineNo = Utils.hex(i, 8);

            const buff = data.slice(i, i+length);
            const hex = [];
            buff.forEach(b => hex.push(Utils.hex(b, padding)));
            let hexStr = hex.join(" ").padEnd(length*(padding+1), " ");

            const ascii = Utils.printable(Utils.byteArrayToChars(buff), false, unixFormat);
            const asciiStr = ascii.padEnd(buff.length, " ");

            if (upperCase) {
                hexStr = hexStr.toUpperCase();

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set length to a positive integer such as 16.
  2. Confirm no decimal point or sign is present.
  3. Use a standard width like 8 or 16 for readable output.

Example fix

// before: args[0] (length) = 0     -> throws
// after:  args[0] (length) = 16    -> renders 16-byte rows
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(Number.isInteger(length) && length >= 1)) {
  throw new Error("Hexdump width must be a positive integer");
}

Type guard

const isValidWidth = w => Number.isInteger(w) && w >= 1;

Try / catch

try { toHexdump(input, [length, ...rest]); }
catch (e) { if (/positive integer/.test(e.message)) { length = 16; } else throw e; }

Prevention

When it happens

Trigger: Setting the length argument to 0, a negative number, or a decimal value such as 16.5.

Common situations: Leaving the field empty or defaulting to 0; pasting a fractional width; misconfiguring the argument upstream.

Related errors


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