{"record":{"id":"3c1a3d72d504c4b8","repo":"apache/beam","slug":"varint-too-long","errorCode":null,"errorMessage":"varint too long","messagePattern":"varint too long","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java","lineNumber":130,"sourceCode":"  /** Decodes a long value from the given stream. */\n  public static long decodeLong(InputStream stream) throws IOException {\n    long result = 0;\n    int shift = 0;\n    int b;\n    do {\n      // Get 7 bits from next byte\n      b = stream.read();\n      if (b < 0) {\n        if (shift == 0) {\n          throw new EOFException();\n        } else {\n          throw new IOException(\"varint not terminated\");\n        }\n      }\n      long bits = b & 0x7F;\n      if (shift >= 64 || (shift == 63 && bits > 1)) {\n        // Out of range\n        throw new IOException(\"varint too long\");\n      }\n      result |= bits << shift;\n      shift += 7;\n    } while ((b & 0x80) != 0);\n    return result;\n  }\n\n  /** Returns the length of the encoding of the given value (in bytes). */\n  public static int getLength(int v) {\n    return CodedOutputStream.computeUInt32SizeNoTag(v);\n  }\n\n  /** Returns the length of the encoding of the given value (in bytes). */\n  public static int getLength(long v) {\n    return CodedOutputStream.computeUInt64SizeNoTag(v);\n  }\n}\n","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java#L112-L148","documentation":"decodeLong stops after it has consumed enough bits for a 64-bit value: once shift >= 64 (or shift == 63 with more than the final bit set), the varint cannot fit in a long. This means the input is either not a valid varint or encodes a value exceeding 64 bits, so an IOException 'varint too long' is thrown.","triggerScenarios":"Decoding a varint with 10+ continuation bytes (value >= 2^64); reading misaligned data where non-varint bytes are interpreted as varint continuation bytes (high bits set), causing the loop to run past 64 bits; corrupt input.","commonSituations":"Reading data written by a different/non-canonical varint encoder (e.g. protobuf of larger width or sign-extended oversized encodings); stream desync after an earlier bad decode; feeding binary data to the wrong parser.","solutions":["Confirm writer and reader use compatible varint encodings (same width, same field order)","Fix stream alignment: the decode position must be exactly at a varint start; re-sync framing","If values can exceed 64 bits, use BigInteger decoding instead of VarInt.decodeLong","Validate/clean the input data source for corruption"],"exampleFix":"// before\nlong v = VarInt.decodeLong(stream); // throws if > 64 bits\n// after\nlong v = readVarIntAsBigInteger(stream).longValueExact(); // handles wider values explicitly","handlingStrategy":"try-catch","validationCode":"// cap continuation bytes before decoding: count bytes with high bit set; if > 10, abort","typeGuard":null,"tryCatchPattern":"try { long v = VarInt.decodeLong(stream); } catch (IOException e) { if (e.getMessage().equals(\"varint too long\")) { /* stream misaligned or invalid encoder */ } }","preventionTips":["Use canonical varint encoding on the writer side","Re-sync stream framing after any decode failure instead of continuing","Reject inputs with more than 10 varint continuation bytes before decoding"],"tags":["java","io","varint","deserialization","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}