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 BrotliDecoder with a non-positive defaultMaxForwardBytes (#18378) · netty/netty@9f1c472

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec/src
    • main/java/io/netty/handler/codec/compression
      • BrotliDecoder.java
    • test/java/io/netty/handler/codec/compression
      • BrotliDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/BrotliDecoder.java‎

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

3535
private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
3636
private static final int DEFAULT_INPUT_BUFFER_SIZE = 8 * 1024;
37+
// Output buffer size while the forwarding threshold is zero or negative: such a threshold forwards every buffer
38+
// that has content, but cannot size one.
39+
static final int DEFAULT_OUTPUT_BUFFER_SIZE = 64 * 1024;
3740

3841
private enum State {
3942
DONE, NEEDS_MORE_INPUT, ERROR
@@ -49,6 +52,7 @@ private enum State {
4952

5053
private final int inputBufferSize;
5154
private final int outputBufferSize;
55+
private final int maxForwardBytes;
5256
private DecoderJNI.Wrapper decoder;
5357
private boolean destroyed;
5458
private boolean needsRead;
@@ -67,7 +71,7 @@ public BrotliDecoder() {
6771
* @param inputBufferSize desired size of the input buffer in bytes
6872
*/
6973
public BrotliDecoder(int inputBufferSize) {
70-
this(inputBufferSize == 0 ? DEFAULT_INPUT_BUFFER_SIZE : inputBufferSize, DEFAULT_MAX_FORWARD_BYTES);
74+
this(inputBufferSize == 0 ? DEFAULT_INPUT_BUFFER_SIZE : inputBufferSize, 0, DEFAULT_MAX_FORWARD_BYTES);
7175
}
7276

7377
/**
@@ -77,8 +81,16 @@ public BrotliDecoder(int inputBufferSize) {
7781
* (produce multiple output buffers if exceeded)
7882
*/
7983
public BrotliDecoder(int inputBufferSize, int outputBufferSize) {
84+
this(ObjectUtil.checkPositive(inputBufferSize, "inputBufferSize"),
85+
ObjectUtil.checkPositive(outputBufferSize, "outputBufferSize"), DEFAULT_MAX_FORWARD_BYTES);
86+
}
87+
88+
// An outputBufferSize of 0 takes defaultMaxForwardBytes instead. Package-private for testing, as that default is
89+
// read from a system property once per JVM.
90+
BrotliDecoder(int inputBufferSize, int outputBufferSize, int defaultMaxForwardBytes) {
8091
this.inputBufferSize = ObjectUtil.checkPositive(inputBufferSize, "inputBufferSize");
81-
this.outputBufferSize = ObjectUtil.checkPositive(outputBufferSize, "outputBufferSize");
92+
this.maxForwardBytes = outputBufferSize > 0 ? outputBufferSize : defaultMaxForwardBytes;
93+
this.outputBufferSize = maxForwardBytes > 0 ? maxForwardBytes : DEFAULT_OUTPUT_BUFFER_SIZE;
8294
}
8395

8496
/**
@@ -107,7 +119,7 @@ private void forwardOutput(ChannelHandlerContext ctx) {
107119
}
108120
accumBuffer.writeBytes(nativeBuffer);
109121
needsRead = false;
110-
if (accumBuffer.readableBytes() >= outputBufferSize) {
122+
if (accumBuffer.isReadable() && accumBuffer.readableBytes() >= maxForwardBytes) {
111123
ctx.fireChannelRead(accumBuffer);
112124
accumBuffer = null;
113125
}
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.params.ParameterizedTest;
3030
import org.junit.jupiter.params.provider.CsvSource;
3131
import org.junit.jupiter.params.provider.MethodSource;
32+
import org.junit.jupiter.params.provider.ValueSource;
3233

3334
import java.io.ByteArrayOutputStream;
3435
import java.io.IOException;
@@ -196,6 +197,40 @@ public void testDecompressionOfTruncatedHighlyCompressibleData() throws Exceptio
196197
assertTrue(readZeros(channel) < 1048576);
197198
}
198199

200+
@ParameterizedTest
201+
@ValueSource(ints = {0, -1})
202+
public void testDecompressionWithNonPositiveDefaultMaxForwardBytes(int defaultMaxForwardBytes) throws Exception {
203+
byte[] compressed = Encoder.compress(new byte[1048576], new Encoder.Parameters().setWindow(10));
204+
205+
EmbeddedChannel ch = new EmbeddedChannel(new BrotliDecoder(8 * 1024, 0, defaultMaxForwardBytes));
206+
try {
207+
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(compressed)));
208+
int parts = 0;
209+
long n = 0;
210+
ByteBuf msg;
211+
while ((msg = ch.readInbound()) != null) {
212+
try {
213+
assertTrue(msg.isReadable());
214+
assertTrue(msg.readableBytes() <= BrotliDecoder.DEFAULT_OUTPUT_BUFFER_SIZE);
215+
for (int i = msg.readerIndex(); i < msg.writerIndex(); i++) {
216+
assertEquals(0, msg.getByte(i));
217+
}
218+
n += msg.readableBytes();
219+
parts++;
220+
} finally {
221+
msg.release();
222+
}
223+
}
224+
assertEquals(1048576, n);
225+
// The 1 KiB window keeps every pull small, so forwarding each pull gives far more parts than buffers of
226+
// DEFAULT_OUTPUT_BUFFER_SIZE would.
227+
assertTrue(parts > 1048576 / BrotliDecoder.DEFAULT_OUTPUT_BUFFER_SIZE, "parts: " + parts);
228+
assertFalse(ch.finish());
229+
} finally {
230+
ch.finishAndReleaseAll();
231+
}
232+
}
233+
199234
private static long readZeros(EmbeddedChannel channel) {
200235
long n = 0;
201236
ByteBuf msg;

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.