From 11daaa986adc4986bd313a2b28890d487f01e3ab Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 23 Dec 2018 18:50:02 -0500 Subject: [PATCH] Add readable checks. --- .../proxy/protocol/ProtocolUtils.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index 8ce5a2cd7..08bd58ca5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -1,6 +1,8 @@ package com.velocitypowered.proxy.protocol; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.network.ProtocolVersion; import io.netty.buffer.ByteBuf; @@ -49,8 +51,10 @@ public enum ProtocolUtils { public static String readString(ByteBuf buf, int cap) { int length = readVarInt(buf); - Preconditions - .checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap); + checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap); + checkState(buf.isReadable(length), + "Trying to read a string that is too long (wanted %s, only have %s)", length, + buf.readableBytes()); String str = buf.toString(buf.readerIndex(), length, StandardCharsets.UTF_8); buf.skipBytes(length); return str; @@ -68,8 +72,10 @@ public enum ProtocolUtils { public static byte[] readByteArray(ByteBuf buf, int cap) { int length = readVarInt(buf); - Preconditions - .checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap); + checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap); + checkState(buf.isReadable(length), + "Trying to read a string that is too long (wanted %s, only have %s)", length, + buf.readableBytes()); byte[] array = new byte[length]; buf.readBytes(array); return array;