gchq/CyberChef · error · OperationError

Type or Length size must be greater than 0

Error message

Type or Length size must be greater than 0

What it means

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.

Source

Thrown at src/core/operations/ParseTLV.mjs:58

            {
                name: "Use BER",
                type: "boolean",
                value: false
            }
        ];
    }

    /**
     * @param {ArrayBuffer} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const [bytesInKey, bytesInLength, basicEncodingRules] = args;
        input = new Uint8Array(input);

        if (bytesInKey <= 0 && bytesInLength <= 0)
            throw new OperationError("Type or Length size must be greater than 0");

        const tlv = new TLVParser(input, { bytesInLength, basicEncodingRules });

        const data = [];

        while (!tlv.atEnd()) {
            const key = bytesInKey ? tlv.getValue(bytesInKey) : undefined;
            const length = tlv.getLength();
            const value = tlv.getValue(length);

            data.push({ key, length, value });
        }

        return data;
    }

}

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set 'Length size' to at least 1 (it describes how many bytes encode the value length).
  2. For keyless LV data keep 'Type/Key size' at 0 and set 'Length size' >= 1.
  3. For KLV data set both 'Type/Key size' and 'Length size' to >= 1.
  4. Confirm the values are numbers and not strings/undefined when calling via the Node API.

Example fix

// before: both sizes 0 -> throws
run(buf, [0, 0, false]);

// after: keyless LV records, 1-byte length
run(buf, [0, 1, false]);
Defensive patterns

Strategy: validation

Validate before calling

const [bytesInKey, bytesInLength] = args;
if (!(bytesInKey > 0) && !(bytesInLength > 0)) {
  throw new Error("Set at least one of Type/Key size or Length size to >= 1");
}
return parseTlv.run(input, args);

Type guard

function isValidTlvSizes(bytesInKey, bytesInLength) {
  return Number.isInteger(bytesInKey) && Number.isInteger(bytesInLength) && (bytesInKey > 0 || bytesInLength > 0);
}

Try / catch

try {
  return parseTlv.run(input, args);
} catch (e) {
  if (e.message === "Type or Length size must be greater than 0") {
    args[1] = Math.max(args[1], 1); // default length size to 1 and retry
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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