Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ static NBTLimiter forBuffer(Object byteBuf, int maxBytes, int maxDepth) {
public void increment(int amount) {
if (amount < 0) {
throw new IllegalArgumentException("Can't increment NBT limiter by negative amount: " + amount);
} else if (this.bytes + amount > maxBytes) {
throw new IllegalArgumentException("NBT size limit reached (" + this.bytes + " + " + amount + " > " + maxBytes + ")");
} else if ((long) this.bytes + amount > maxBytes) {
throw new IllegalArgumentException("NBT size limit reached (" + ((long) this.bytes + amount) + " > " + maxBytes + ")");
}
this.bytes += amount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.retrooper.packetevents.test;

import com.github.retrooper.packetevents.protocol.nbt.NBTLimiter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class NBTLimiterTest {

@Test
public void testSizeAccountingDoesNotOverflow() {
NBTLimiter limiter = NBTLimiter.forBuffer(new Object(), Integer.MAX_VALUE - 1);
limiter.increment(Integer.MAX_VALUE - 2);

// The sum exceeds the configured limit and also overflows a signed int.
assertThrows(IllegalArgumentException.class, () -> limiter.increment(4));
}

@Test
public void testExactLimitAllowed() {
NBTLimiter limiter = NBTLimiter.forBuffer(new Object(), 64);
limiter.increment(32);
limiter.increment(32);

assertThrows(IllegalArgumentException.class, () -> limiter.increment(1));
}

@Test
public void testNegativeIncrementsAreRejected() {
NBTLimiter limiter = NBTLimiter.forBuffer(new Object(), 64);
assertThrows(IllegalArgumentException.class, () -> limiter.increment(-1));
}
}
Loading