GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix ZstdDecoder and ZstdDecompressor with a non-positive defaultMaxFo… · netty/netty@5baf6f3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src
    • main/java/io/netty/handler/codec/compression
      • ZstdDecoder.java
      • ZstdDecompressor.java
    • test/java/io/netty/handler/codec/compression
      • ZstdDecoderTest.java
      • ZstdDecompressorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java
+14-3Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public final class ZstdDecoder extends ByteToMessageDecoder {
4141
}
4242

4343
private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
44+
// Output buffer size while maxForwardBytes is zero or negative: such a threshold forwards every buffer that has
45+
// content, but cannot size one. ZstdDecompressor uses it too.
46+
static final int DEFAULT_OUTPUT_BUFFER_SIZE = 64 * 1024;
4447
/**
4548
* Default maximum size of a single output buffer, in bytes (4 MiB).
4649
*/
@@ -53,6 +56,7 @@ public final class ZstdDecoder extends ByteToMessageDecoder {
5356
private static final int MIN_WINDOW_LOG = 10;
5457
private static final int MAX_WINDOW_LOG = 31;
5558
private final int maxForwardBytes;
59+
private final int outputBufferSize;
5660
private final int maxWindowLog;
5761
private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream();
5862
private ZstdInputStreamNoFinalizer zstdIs;
@@ -103,8 +107,14 @@ public ZstdDecoder(int maximumAllocationSize) {
103107
* decoder may allocate per stream.
104108
*/
105109
public ZstdDecoder(int maximumAllocationSize, int maxWindowLog) {
110+
this(maximumAllocationSize, maxWindowLog, DEFAULT_MAX_FORWARD_BYTES);
111+
}
112+
113+
// Package-private for testing, as the default is read from a system property once per JVM.
114+
ZstdDecoder(int maximumAllocationSize, int maxWindowLog, int defaultMaxForwardBytes) {
106115
ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize");
107-
this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : DEFAULT_MAX_FORWARD_BYTES;
116+
this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : defaultMaxForwardBytes;
117+
this.outputBufferSize = maxForwardBytes > 0 ? maxForwardBytes : DEFAULT_OUTPUT_BUFFER_SIZE;
108118
this.maxWindowLog = ObjectUtil.checkInRange(maxWindowLog, MIN_WINDOW_LOG, MAX_WINDOW_LOG, "maxWindowLog");
109119
}
110120

@@ -142,12 +152,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
142152
if (outBuffer == null) {
143153
// The frame content size is read from the (untrusted) frame header, so never use it
144154
// to size the buffer beyond what we forward in one go.
145-
outBuffer = ctx.alloc().heapBuffer((int) Math.min(maxForwardBytes, uncompressedLength));
155+
outBuffer = ctx.alloc().heapBuffer((int) Math.min(outputBufferSize, uncompressedLength));
146156
}
147157
do {
148158
w = outBuffer.writeBytes(zstdIs, outBuffer.writableBytes());
149159
} while (w > 0 && outBuffer.isWritable());
150-
if (!outBuffer.isWritable() || outBuffer.readableBytes() >= maxForwardBytes) {
160+
if (outBuffer.isReadable() &&
161+
(!outBuffer.isWritable() || outBuffer.readableBytes() >= maxForwardBytes)) {
151162
needsRead = false;
152163
ctx.fireChannelRead(outBuffer);
153164
outBuffer = null;
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecompressor.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecompressor.java
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,27 @@ public final class ZstdDecompressor implements Decompressor {
4141
private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
4242

4343
private final ByteBufAllocator allocator;
44+
private final int outputBufferSize;
4445

4546
private final MutableByteBufInputStream mutableInput = new MutableByteBufInputStream();
4647
private final ZstdInputStreamNoFinalizer output;
4748

4849
ZstdDecompressor(Builder builder, ByteBufAllocator allocator) {
50+
this(builder, allocator, DEFAULT_MAX_FORWARD_BYTES);
51+
}
52+
53+
// Package-private for testing, as the default is read from a system property once per JVM.
54+
ZstdDecompressor(Builder builder, ByteBufAllocator allocator, int defaultMaxForwardBytes) {
4955
// Don't use static here as we want to still allow to load the classes.
5056
try {
5157
Zstd.ensureAvailability();
5258
} catch (Throwable throwable) {
5359
throw new ExceptionInInitializerError(throwable);
5460
}
5561
this.allocator = allocator;
62+
// A threshold of zero or less cannot size a buffer.
63+
this.outputBufferSize = defaultMaxForwardBytes > 0 ?
64+
defaultMaxForwardBytes : ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE;
5665
ZstdInputStreamNoFinalizer output = null;
5766
try {
5867
output = new ZstdInputStreamNoFinalizer(mutableInput);
@@ -112,9 +121,9 @@ public void endOfInput() throws DecompressionException {
112121

113122
@Override
114123
public ByteBuf takeOutput() throws DecompressionException {
115-
ByteBuf buf = allocator.buffer(DEFAULT_MAX_FORWARD_BYTES, DEFAULT_MAX_FORWARD_BYTES);
124+
ByteBuf buf = allocator.buffer(outputBufferSize, outputBufferSize);
116125
try {
117-
buf.writeBytes(output, DEFAULT_MAX_FORWARD_BYTES);
126+
buf.writeBytes(output, outputBufferSize);
118127
} catch (IOException e) {
119128
buf.release();
120129
throw new DecompressionException(e);
Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecoderTest.java
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.netty.buffer.Unpooled;
2323
import io.netty.channel.embedded.EmbeddedChannel;
2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527

2628
import java.util.Random;
2729

@@ -177,6 +179,41 @@ public void testDecompressLargeFrameWhenUnlimited() {
177179
}
178180
}
179181

182+
@ParameterizedTest
183+
@ValueSource(ints = {0, -1})
184+
public void testDecompressWhenUnlimitedWithNonPositiveDefaultMaxForwardBytes(int defaultMaxForwardBytes) {
185+
byte[] payload = new byte[1024 * 1024];
186+
new Random(12345L).nextBytes(payload);
187+
byte[] compressed = Zstd.compress(payload);
188+
189+
EmbeddedChannel ch = new EmbeddedChannel(
190+
new ZstdDecoder(0, ZstdDecoder.DEFAULT_MAX_WINDOW_LOG, defaultMaxForwardBytes));
191+
try {
192+
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(compressed)));
193+
194+
ByteBuf acc = Unpooled.buffer();
195+
try {
196+
ByteBuf b;
197+
while ((b = ch.readInbound()) != null) {
198+
try {
199+
assertTrue(b.isReadable());
200+
assertTrue(b.readableBytes() <= ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE);
201+
acc.writeBytes(b);
202+
} finally {
203+
b.release();
204+
}
205+
}
206+
byte[] actual = new byte[acc.readableBytes()];
207+
acc.readBytes(actual);
208+
assertArrayEquals(payload, actual);
209+
} finally {
210+
acc.release();
211+
}
212+
} finally {
213+
ch.finishAndReleaseAll();
214+
}
215+
}
216+
180217
private static byte[] compressWithWindowLog(byte[] data, int windowLog) {
181218
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
182219
ctx.setLevel(Zstd.defaultCompressionLevel());
Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecompressorTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecompressorTest.java
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.netty.buffer.Unpooled;
2323
import io.netty.channel.ChannelHandler;
2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527

2628
import java.util.Arrays;
2729
import java.util.Random;
@@ -30,6 +32,7 @@
3032
import static org.junit.jupiter.api.Assertions.assertEquals;
3133
import static org.junit.jupiter.api.Assertions.assertFalse;
3234
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3336

3437
public class ZstdDecompressorTest extends AbstractDecompressorTest {
3538
@Override
@@ -89,6 +92,40 @@ public void testTruncatedFrameIsRejectedAfterEndOfInput() {
8992
assertThrows(DecompressionException.class, () -> decompress(truncated, 20));
9093
}
9194

95+
@ParameterizedTest
96+
@ValueSource(ints = {0, -1})
97+
public void testDecompressWithNonPositiveDefaultMaxForwardBytes(int defaultMaxForwardBytes)
98+
throws DecompressionException {
99+
byte[] payload = new byte[1024 * 1024];
100+
new Random(12345L).nextBytes(payload);
101+
byte[] compressed = Zstd.compress(payload);
102+
103+
ByteBuf output = Unpooled.buffer();
104+
try (Decompressor decompressor = new DefensiveDecompressor(new ZstdDecompressor(
105+
ZstdDecompressor.builder(), ByteBufAllocator.DEFAULT, defaultMaxForwardBytes))) {
106+
assertEquals(Decompressor.Status.NEED_INPUT, decompressor.status());
107+
decompressor.addInput(Unpooled.wrappedBuffer(compressed));
108+
// The body takes 16 outputs of 64 KiB, so the bound only stops a decompressor that makes no progress.
109+
for (int i = 0; i < 1024 && decompressor.status() == Decompressor.Status.NEED_OUTPUT; i++) {
110+
ByteBuf b = decompressor.takeOutput();
111+
try {
112+
assertTrue(b.readableBytes() <= ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE);
113+
output.writeBytes(b);
114+
} finally {
115+
b.release();
116+
}
117+
}
118+
assertEquals(Decompressor.Status.NEED_INPUT, decompressor.status());
119+
decompressor.endOfInput();
120+
assertEquals(Decompressor.Status.COMPLETE, decompressor.status());
121+
byte[] actual = new byte[output.readableBytes()];
122+
output.readBytes(actual);
123+
assertArrayEquals(payload, actual);
124+
} finally {
125+
output.release();
126+
}
127+
}
128+
92129
private static byte[] decompress(byte[] compressed, int maxWindowLog) throws DecompressionException {
93130
ByteBuf input = Unpooled.wrappedBuffer(compressed);
94131
ByteBuf output = Unpooled.buffer();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.