From f9462cc949a0350fb5c4f2b6bddf9f7f6cc370d5 Mon Sep 17 00:00:00 2001 From: dmitrybugakov Date: Mon, 4 Sep 2023 15:05:34 +0200 Subject: [PATCH] [CLEANUP]: Improve readability, Fix potential Overflow(Maximum 10 bytes for a 64-bit number) --- .../housepower/serde/BinaryDeserializer.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/serde/BinaryDeserializer.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/serde/BinaryDeserializer.java index 456ad696..ac93903d 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/serde/BinaryDeserializer.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/serde/BinaryDeserializer.java @@ -35,18 +35,23 @@ public BinaryDeserializer(BuffedReader buffedReader, boolean enableCompress) { switcher = new Switcher<>(compressedReader, buffedReader); } + /** + * Reads VarInt from binary stream; uses lower 7 bits for value, 8th bit as continuation flag. + * + * @return Parsed VarInt. + * @throws IOException On read error. + */ public long readVarInt() throws IOException { - int number = 0; - for (int i = 0; i < 9; i++) { - int byt = switcher.get().readBinary(); - - number |= (byt & 0x7F) << (7 * i); - - if ((byt & 0x80) == 0) { - break; + long result = 0; + for (int i = 0; i < 10; i++) { + int currentByte = switcher.get().readBinary(); + long valueChunk = currentByte & 0x7F; + result |= (valueChunk << (7 * i)); + if ((currentByte & 0x80) == 0) { + return result; } } - return number; + throw new IOException("Malformed VarInt: too long"); } @SuppressWarnings("PointlessBitwiseExpression")