{"record":{"id":"1010f3ffea9d99b2","repo":"gchq/CyberChef","slug":"invalid-key-length-required-to-be-at-least-2","errorCode":null,"errorMessage":"Invalid key length, required to be at least 2.","messagePattern":"Invalid key length, required to be at least 2\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/operations/GenerateDeBruijnSequence.mjs","lineNumber":62,"sourceCode":"     * @returns {string}\n     */\n    run(input, args) {\n        const [k, n] = args;\n\n        if (k < 2 || k > 9) {\n            throw new OperationError(\"Invalid alphabet size, required to be between 2 and 9 (inclusive).\");\n        }\n\n        if (!Number.isInteger(k)) {\n            throw new OperationError(\"Invalid alphabet size, required to be integer.\");\n        }\n\n        if (!Number.isInteger(n)) {\n            throw new OperationError(\"Invalid key length, required to be integer.\");\n        }\n\n        if (n < 2) {\n            throw new OperationError(\"Invalid key length, required to be at least 2.\");\n        }\n\n        if (Math.pow(k, n) > 50000) {\n            throw new OperationError(\"Too many permutations, please reduce k^n to under 50,000.\");\n        }\n\n        const a = new Array(k * n).fill(0);\n        const sequence = [];\n\n        (function db(t = 1, p = 1) {\n            if (t > n) {\n                if (n % p !== 0) return;\n                for (let j = 1; j <= p; j++) {\n                    sequence.push(a[j]);\n                }\n                return;\n            }\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GenerateDeBruijnSequence.mjs#L44-L80","documentation":"Fourth validation in Generate De Bruijn Sequence: requires key length `n` to be at least 2. A De Bruijn sequence of subsequence length 1 is trivial and excluded by design; the generator also indexes arrays based on n positions.","triggerScenarios":"Setting Key length (n) to 0, 1, or a negative number.","commonSituations":"Misreading n as alphabet size; testing edge cases with n=1.","solutions":["Set Key length (n) to an integer >= 2.","Keep n large enough that the generated sequence is meaningful for your use case."],"exampleFix":"// before\nargs = [2, 1];\n// after\nargs = [2, 3];","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(n) || n < 2) throw new Error(\"Key length n must be an integer >= 2\");","typeGuard":"/** @param {number} n */\nfunction isValidKeyLength(n){return Number.isInteger(n) && n >= 2;}","tryCatchPattern":null,"preventionTips":["Keep n >= 2.","Validate integer-ness before the range check when scripting."],"tags":["combinatorics","de-bruijn","argument-validation","bounds"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}