Skip to content

Commit

Permalink
Properly calculate the size of INewChunk#getIntBiomeArray
Browse files Browse the repository at this point in the history
This array is written to the PacketBuffer using
PacketBuffer#writeVarIntArray, which writes both the array size and
individual array elements as VarInts. However, JEID's calculation of the
array size was leaving out the array length prefix, and assuming that
each array element was exactly four bytes. Since a VarInt-encoded
integer can range from 1 to 5 bytes, the calculation of the array size
could end up being smaller than the actual number of bytes written,
causing a crash.

This commit makes JEID properly calculate the size of getIntBiomeArray,
ensuring that the allocated buffer size always matches the number of
bytes written.
  • Loading branch information
Aaron1011 authored and Runemoro committed Jul 25, 2018
1 parent 2ee4547 commit 92a54da
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,25 @@ public boolean getIsFullChunk(SPacketChunkData packet) {
return false;
}

@Redirect(method = "calculateChunkSize", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/Chunk;getBiomeArray()[B"))
private byte[] getBiomeArray(Chunk chunk) {
INewChunk newChunk = (INewChunk) chunk;
return new byte[newChunk.getIntBiomeArray().length * 4];
@Inject(method = "calculateChunkSize", at = @At(value = "RETURN"), cancellable = true)
public void onReturn(Chunk chunkIn, boolean p_189556_2_, int p_189556_3_, CallbackInfoReturnable<Integer> ci) {
if (this.isFullChunk()) {
int size = ci.getReturnValue();

// First, we subtract off the length added by Vanilla (the line 'i += chunkIn.getBiomeArray().length;')
size -= chunkIn.getBiomeArray().length;

// Now, we add on the actual length of the VarIntArray we're going to be writing in extractChunkData
size += this.getVarIntArraySize(((INewChunk) chunkIn).getIntBiomeArray());
ci.setReturnValue(size);
}
}

private int getVarIntArraySize(int[] array) {
int size = PacketBuffer.getVarIntSize(array.length);
for (int i: array) {
size += PacketBuffer.getVarIntSize(i);
}
return size;
}
}

0 comments on commit 92a54da

Please sign in to comment.