{"record":{"id":"294c14cd640a3ec5","repo":"gchq/CyberChef","slug":"width-must-be-a-positive-integer","errorCode":null,"errorMessage":"Width must be a positive integer","messagePattern":"Width must be a positive integer","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToHexdump.mjs","lineNumber":67,"sourceCode":"                \"name\": \"UNIX format\",\n                \"type\": \"boolean\",\n                \"value\": false\n            }\n        ];\n    }\n\n    /**\n     * @param {ArrayBuffer} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const data = new Uint8Array(input);\n        const [length, upperCase, includeFinalLength, unixFormat] = args;\n        const padding = 2;\n\n        if (length < 1 || Math.round(length) !== length)\n            throw new OperationError(\"Width must be a positive integer\");\n\n        if (length > MAX_WIDTH)\n            throw new OperationError(`Width must be no more than ${MAX_WIDTH}`);\n\n        const lines = [];\n        for (let i = 0; i < data.length; i += length) {\n            let lineNo = Utils.hex(i, 8);\n\n            const buff = data.slice(i, i+length);\n            const hex = [];\n            buff.forEach(b => hex.push(Utils.hex(b, padding)));\n            let hexStr = hex.join(\" \").padEnd(length*(padding+1), \" \");\n\n            const ascii = Utils.printable(Utils.byteArrayToChars(buff), false, unixFormat);\n            const asciiStr = ascii.padEnd(buff.length, \" \");\n\n            if (upperCase) {\n                hexStr = hexStr.toUpperCase();","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToHexdump.mjs#L49-L85","documentation":"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.","triggerScenarios":"Setting the length argument to 0, a negative number, or a decimal value such as 16.5.","commonSituations":"Leaving the field empty or defaulting to 0; pasting a fractional width; misconfiguring the argument upstream.","solutions":["Set length to a positive integer such as 16.","Confirm no decimal point or sign is present.","Use a standard width like 8 or 16 for readable output."],"exampleFix":"// before: args[0] (length) = 0     -> throws\n// after:  args[0] (length) = 16    -> renders 16-byte rows","handlingStrategy":"type-guard","validationCode":"if (!(Number.isInteger(length) && length >= 1)) {\n  throw new Error(\"Hexdump width must be a positive integer\");\n}","typeGuard":"const isValidWidth = w => Number.isInteger(w) && w >= 1;","tryCatchPattern":"try { toHexdump(input, [length, ...rest]); }\ncatch (e) { if (/positive integer/.test(e.message)) { length = 16; } else throw e; }","preventionTips":["Default the width field to 16.","Validate with Number.isInteger at the form layer.","Reject fractional or zero widths early."],"tags":["hexdump","validation","integer","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}