{"record":{"id":"25a8f3063bc5388c","repo":"gchq/CyberChef","slug":"negative-values-cannot-be-represented-as-varint","errorCode":null,"errorMessage":"Negative values cannot be represented as VarInt","messagePattern":"Negative values cannot be represented as VarInt","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/VarIntEncode.mjs","lineNumber":40,"sourceCode":"        this.name = \"VarInt Encode\";\n        this.module = \"Default\";\n        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.\";\n        this.infoURL = \"https://developers.google.com/protocol-buffers/docs/encoding#varints\";\n        this.inputType = \"string\";\n        this.outputType = \"byteArray\";\n        this.args = [];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {byteArray}\n     */\n    run(input, args) {\n        try {\n            if (typeof BigInt === \"function\") {\n                let value = BigInt(input);\n                if (value < 0) throw new OperationError(\"Negative values cannot be represented as VarInt\");\n                const result = [];\n                while (value >= 0x80) {\n                    result.push(Number(value & BigInt(0x7f)) | 0x80);\n                    value >>= BigInt(7);\n                }\n                result.push(Number(value));\n                return result;\n            } else {\n                return Protobuf.varIntEncode(Number(input));\n            }\n        } catch (err) {\n            throw new OperationError(err);\n        }\n    }\n\n}\n\nexport default VarIntEncode;","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/VarIntEncode.mjs#L22-L58","documentation":"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.","triggerScenarios":"Input string representing a negative integer such as '-1' or '-42'. BigInt(input) succeeds and the value < 0 check trips before the encode loop.","commonSituations":"Passing signed/offset data, negative timestamps, or subtracted values into an operation that expects unsigned counts/IDs.","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."],"exampleFix":"// before\nvarIntEncode(\"-5\");\n// after\nvarIntEncode(\"5\");","handlingStrategy":"validation","validationCode":"const v = BigInt(input);\nif (v < 0n) throw new Error(\"VarInt cannot encode negative values\");","typeGuard":"function isNonNegativeBigInt(s) { try { return BigInt(s) >= 0n; } catch { return false; } }","tryCatchPattern":null,"preventionTips":["Reject or transform negative inputs before encoding.","Use two's-complement fixed-width for signed values."],"tags":["protobuf","varint","validation","argument-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}