{"record":{"id":"c9a3f9ea9526cf55","repo":"apache/beam","slug":"varint-overflow","errorCode":null,"errorMessage":"varint overflow ","messagePattern":"varint overflow ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java","lineNumber":107,"sourceCode":"      stream.write((byte) v);\n      return;\n    }\n    stream.write((byte) (v | 0x80));\n    v >>>= 7;\n    if ((v & ~0x7F) == 0) {\n      stream.write((byte) v);\n      return;\n    }\n    stream.write((byte) (v | 0x80));\n    v >>>= 7;\n    stream.write((byte) v);\n  }\n\n  /** Decodes an integer value from the given stream. */\n  public static int decodeInt(InputStream stream) throws IOException {\n    long r = decodeLong(stream);\n    if (r < 0 || r >= 1L << 32) {\n      throw new IOException(\"varint overflow \" + r);\n    }\n    return (int) r;\n  }\n\n  /** 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        }","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java#L89-L125","documentation":"decodeInt reads a base-128 varint from a stream and requires the value to fit in a signed 32-bit int (0 <= r < 2^32 as an unsigned reading). If the decoded long is negative or >= 2^32, the value cannot be represented as an int and an IOException is thrown.","triggerScenarios":"Reading a varint that was encoded from a long value outside the int range (negative longs, or values >= 2^32), or reading a corrupted/misaligned stream where varint bytes shift decoding into a huge value.","commonSituations":"Decoding data written with VarInt.encodeLong (or another encoder allowing 64-bit values) with decodeInt; deserializing a stream whose field boundaries were lost so bytes of two varints merge into one; schema/version mismatches between writer and reader.","solutions":["Use VarInt.decodeLong instead of decodeInt when the value may exceed int range","Fix the writer side to use encodeInt, or encode values that fit in 32 bits","Verify stream alignment: re-check the serialization framing so decoding starts at the correct byte","Validate decoded values at write time before persisting"],"exampleFix":"// before\nint n = VarInt.decodeInt(stream);\n// after\nlong r = VarInt.decodeLong(stream);\nif (r < 0 || r >= 1L << 32) throw new IOException(\"value out of int range: \" + r);\nint n = (int) r;","handlingStrategy":"validation","validationCode":"// on the writer side, before encoding:\nif (value < 0 || value >= 1L << 32) throw new IllegalArgumentException(\"value does not fit int varint: \" + value);","typeGuard":null,"tryCatchPattern":"try { int n = VarInt.decodeInt(stream); } catch (IOException e) { if (e.getMessage().startsWith(\"varint overflow\")) { /* re-read with decodeLong */ } }","preventionTips":["Match decodeInt/decodeLong to the encodeInt/encodeLong used by the writer","Range-check values before persisting them as int varints","Keep writer/reader schemas in sync"],"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"}