{"record":{"id":"e55815705fc3f28a","repo":"gchq/CyberChef","slug":"type-or-length-size-must-be-greater-than-0","errorCode":null,"errorMessage":"Type or Length size must be greater than 0","messagePattern":"Type or Length size must be greater than 0","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ParseTLV.mjs","lineNumber":58,"sourceCode":"            {\n                name: \"Use BER\",\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 [bytesInKey, bytesInLength, basicEncodingRules] = args;\n        input = new Uint8Array(input);\n\n        if (bytesInKey <= 0 && bytesInLength <= 0)\n            throw new OperationError(\"Type or Length size must be greater than 0\");\n\n        const tlv = new TLVParser(input, { bytesInLength, basicEncodingRules });\n\n        const data = [];\n\n        while (!tlv.atEnd()) {\n            const key = bytesInKey ? tlv.getValue(bytesInKey) : undefined;\n            const length = tlv.getLength();\n            const value = tlv.getValue(length);\n\n            data.push({ key, length, value });\n        }\n\n        return data;\n    }\n\n}\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ParseTLV.mjs#L40-L76","documentation":"Parse TLV converts a Type-Length-Value stream into JSON and needs to know how many bytes encode the Type/Key and the Length. The guard throws only when BOTH 'Type/Key size' and 'Length size' are <= 0 (note the && operator): with both zero there is nothing to read for either field, so parsing cannot proceed. A single zero value is allowed — Type/Key size 0 means keyless Length-Value (LV) records.","triggerScenarios":"Setting both the 'Type/Key size' and 'Length size' operation arguments to 0 or a negative number, e.g. run(buf, [0, 0, false]) or run(buf, [-1, 0, false]). Programmatic callers that clear both fields or pass undefined/default to a falsy 0 also hit it.","commonSituations":"User clears both numeric ingredients expecting the parser to infer sizes; recipe import where the number args were lost; calling the op with placeholder [0,0] during testing.","solutions":["Set 'Length size' to at least 1 (it describes how many bytes encode the value length).","For keyless LV data keep 'Type/Key size' at 0 and set 'Length size' >= 1.","For KLV data set both 'Type/Key size' and 'Length size' to >= 1.","Confirm the values are numbers and not strings/undefined when calling via the Node API."],"exampleFix":"// before: both sizes 0 -> throws\nrun(buf, [0, 0, false]);\n\n// after: keyless LV records, 1-byte length\nrun(buf, [0, 1, false]);","handlingStrategy":"validation","validationCode":"const [bytesInKey, bytesInLength] = args;\nif (!(bytesInKey > 0) && !(bytesInLength > 0)) {\n  throw new Error(\"Set at least one of Type/Key size or Length size to >= 1\");\n}\nreturn parseTlv.run(input, args);","typeGuard":"function isValidTlvSizes(bytesInKey, bytesInLength) {\n  return Number.isInteger(bytesInKey) && Number.isInteger(bytesInLength) && (bytesInKey > 0 || bytesInLength > 0);\n}","tryCatchPattern":"try {\n  return parseTlv.run(input, args);\n} catch (e) {\n  if (e.message === \"Type or Length size must be greater than 0\") {\n    args[1] = Math.max(args[1], 1); // default length size to 1 and retry\n  }\n  throw e;\n}","preventionTips":["Default Length size to >= 1 in any recipe/UI.","Allow Type/Key size 0 only for keyless LV data, never both zero.","Validate both args are positive integers except for an intentional 0."],"tags":["parsing","tlv","input-validation","arguments"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}