{"record":{"id":"f7621aa2d8137539","repo":"gchq/CyberChef","slug":"invalid-input-f7621a","errorCode":null,"errorMessage":"Invalid input","messagePattern":"Invalid input","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToBCD.mjs","lineNumber":61,"sourceCode":"                \"type\": \"boolean\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Output format\",\n                \"type\": \"option\",\n                \"value\": FORMAT\n            }\n        ];\n    }\n\n    /**\n     * @param {BigNumber} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        if (input.isNaN())\n            throw new OperationError(\"Invalid input\");\n        if (!input.integerValue(BigNumber.ROUND_DOWN).isEqualTo(input))\n            throw new OperationError(\"Fractional values are not supported by BCD\");\n\n        const encoding = ENCODING_LOOKUP[args[0]],\n            packed = args[1],\n            signed = args[2],\n            outputFormat = args[3];\n\n        // Split input number up into separate digits\n        const digits = input.toFixed().split(\"\");\n\n        if (digits[0] === \"-\" || digits[0] === \"+\") {\n            digits.shift();\n        }\n\n        let nibbles = [];\n\n        digits.forEach(d => {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToBCD.mjs#L43-L79","documentation":"To BCD rejects input that is not a valid number — specifically when BigNumber.isNaN() is true. BCD (Binary-Coded Decimal) encodes decimal digits, so a non-numeric or unparseable input cannot be processed.","triggerScenarios":"Feeding the operation a BigNumber that is NaN, e.g. a string like \"abc\" or \"\" parsed upstream into an invalid number, or any value the BigNumber layer considers NaN.","commonSituations":"Chaining To BCD after an operation that emits text rather than a number; passing an empty or whitespace-only input; locale-formatted numbers with commas that fail to parse.","solutions":["Ensure the input is a clean numeric value (digits and optional leading sign only).","Precede To BCD with a 'To Decimal Number' / parsing step to coerce the value.","Strip non-numeric characters from the input."],"exampleFix":"// before: input = \"12a3\"  -> NaN -> throws\n// after:  input = 123     -> valid number, BCD encodes digits 1,2,3","handlingStrategy":"type-guard","validationCode":"if (!BigNumber.isBigNumber(input) || input.isNaN()) {\n  throw new Error(\"To BCD requires a finite number\");\n}","typeGuard":"const isValidBcdInput = v => BigNumber.isBigNumber(v) && !v.isNaN();","tryCatchPattern":"try { toBcd(input, ...args); }\ncatch (e) { if (/Invalid input/.test(e.message)) { /* parse/replace input to a number */ } else throw e; }","preventionTips":["Parse upstream output to a BigNumber before To BCD.","Strip non-numeric characters from input.","Reject NaN at the data boundary."],"tags":["bcd","number","validation","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}