13
0
geforkt von Mirrors/Velocity

Add readable checks.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-12-23 18:50:02 -05:00
Ursprung 3a87e47796
Commit 11daaa986a

Datei anzeigen

@ -1,6 +1,8 @@
package com.velocitypowered.proxy.protocol; 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.util.GameProfile;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -49,8 +51,10 @@ public enum ProtocolUtils {
public static String readString(ByteBuf buf, int cap) { public static String readString(ByteBuf buf, int cap) {
int length = readVarInt(buf); 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); String str = buf.toString(buf.readerIndex(), length, StandardCharsets.UTF_8);
buf.skipBytes(length); buf.skipBytes(length);
return str; return str;
@ -68,8 +72,10 @@ public enum ProtocolUtils {
public static byte[] readByteArray(ByteBuf buf, int cap) { public static byte[] readByteArray(ByteBuf buf, int cap) {
int length = readVarInt(buf); 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]; byte[] array = new byte[length];
buf.readBytes(array); buf.readBytes(array);
return array; return array;