Skip to content

Commit

Permalink
[CLEANUP]: Improve readability, Fix potential Overflow(Maximum 10 byt…
Browse files Browse the repository at this point in the history
…es for a 64-bit number) (#449)
  • Loading branch information
dmitrybugakov authored Sep 4, 2023
1 parent 9c2e77a commit fe79d3e
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit fe79d3e

Please sign in to comment.