From 8e0adb33521e47f51d92b09b0759a80268ee811e Mon Sep 17 00:00:00 2001 From: Davy Landman Date: Tue, 13 Feb 2024 12:57:24 +0100 Subject: [PATCH] Fix bug where a large file (>1.2GB) would cause a int overflow The reasons was to avoid large buffers getting allocated for small files, but that hurt big files in the end --- .../vallang/io/binary/util/DirectZstdInputStream.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/usethesource/vallang/io/binary/util/DirectZstdInputStream.java b/src/main/java/io/usethesource/vallang/io/binary/util/DirectZstdInputStream.java index 8505ba270..efa716355 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/util/DirectZstdInputStream.java +++ b/src/main/java/io/usethesource/vallang/io/binary/util/DirectZstdInputStream.java @@ -28,7 +28,11 @@ protected ByteBuffer refill(ByteBuffer torefill) throws IOException { private static ByteBuffer constructDecompressedBuffer(ByteBufferInputStream oriStream) { int compressedSize = oriStream.getByteBuffer().remaining(); - ByteBuffer result = DirectByteBufferCache.getInstance().get(Math.min(compressedSize * 2, ZstdDirectBufferDecompressingStream.recommendedTargetBufferSize())); + int bufferSize = ZstdDirectBufferDecompressingStream.recommendedTargetBufferSize(); + if (bufferSize > compressedSize) { + bufferSize = Math.min(compressedSize * 2, bufferSize); + } + ByteBuffer result = DirectByteBufferCache.getInstance().get(bufferSize); result.limit(0); // delay compression for first read return result; }