ByteBuffers: check String length against max length

This commit is contained in:
Bixilon 2020-12-03 11:41:48 +01:00
parent ee37e092cb
commit 9f19b460fb
2 changed files with 13 additions and 3 deletions

View File

@ -101,6 +101,10 @@ public class InByteBuffer {
}
public String readString() {
byte[] data = readBytes(readVarInt());
if (data.length > ProtocolDefinition.STRING_MAX_LEN) {
throw new IllegalArgumentException(String.format("String max string length exceeded %d > %d", data.length, ProtocolDefinition.STRING_MAX_LEN));
}
return new String(readBytes(readVarInt()), StandardCharsets.UTF_8);
}
@ -152,7 +156,8 @@ public class InByteBuffer {
int byteCount = 0;
int result = 0;
byte read;
do {
do
{
read = readByte();
result |= (read & 0x7F) << (7 * byteCount);
byteCount++;

View File

@ -85,6 +85,9 @@ public class OutByteBuffer {
}
public void writeString(String string) {
if (string.length() > ProtocolDefinition.STRING_MAX_LEN) {
throw new IllegalArgumentException(String.format("String max string length exceeded %d > %d", string.length(), ProtocolDefinition.STRING_MAX_LEN));
}
writeVarInt(string.length());
writeBytes(string.getBytes(StandardCharsets.UTF_8));
}
@ -147,7 +150,8 @@ public class OutByteBuffer {
public void writeVarInt(int value) {
// thanks https://wiki.vg/Protocol#VarInt_and_VarLong
do {
do
{
byte temp = (byte) (value & 0x7F);
value >>>= 7;
if (value != 0) {
@ -160,7 +164,8 @@ public class OutByteBuffer {
public void prefixVarInt(int value) {
int count = 0;
// thanks https://wiki.vg/Protocol#VarInt_and_VarLong
do {
do
{
byte temp = (byte) (value & 0x7F);
value >>>= 7;
if (value != 0) {