gchq/CyberChef · error · OperationError
Negative values cannot be represented as VarInt
Error message
Negative values cannot be represented as VarInt
What it means
Thrown by 'VarInt Encode' when BigInt(input) parses successfully but the value is negative. VarInt (protobuf-style) encodes unsigned integers only, so negative numbers have no valid encoding.
Source
Thrown at src/core/operations/VarIntEncode.mjs:40
this.name = "VarInt Encode";
this.module = "Default";
this.description = "Encodes a Vn integer as a VarInt. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf.";
this.infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints";
this.inputType = "string";
this.outputType = "byteArray";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
try {
if (typeof BigInt === "function") {
let value = BigInt(input);
if (value < 0) throw new OperationError("Negative values cannot be represented as VarInt");
const result = [];
while (value >= 0x80) {
result.push(Number(value & BigInt(0x7f)) | 0x80);
value >>= BigInt(7);
}
result.push(Number(value));
return result;
} else {
return Protobuf.varIntEncode(Number(input));
}
} catch (err) {
throw new OperationError(err);
}
}
}
export default VarIntEncode;View on GitHub (pinned to 4290ea7539)
Solutions
- Provide a non-negative integer string as input.
- If you need signed values, convert to an unsigned representation (e.g. two's complement to a fixed width) before encoding.
- Sanitise upstream output to clamp/abs negative values when semantically appropriate.
Example fix
// before
varIntEncode("-5");
// after
varIntEncode("5"); Defensive patterns
Strategy: validation
Validate before calling
const v = BigInt(input);
if (v < 0n) throw new Error("VarInt cannot encode negative values"); Type guard
function isNonNegativeBigInt(s) { try { return BigInt(s) >= 0n; } catch { return false; } } Prevention
- Reject or transform negative inputs before encoding.
- Use two's-complement fixed-width for signed values.
When it happens
Trigger: Input string representing a negative integer such as '-1' or '-42'. BigInt(input) succeeds and the value < 0 check trips before the encode loop.
Common situations: Passing signed/offset data, negative timestamps, or subtracted values into an operation that expects unsigned counts/IDs.
Related errors
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/25a8f3063bc5388c.
Report an issue: GitHub.