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 JZlibDecoder truncating highly compressible streams (#17392) · netty/netty@8b72847

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JZlibDecoder.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
public class JZlibDecoder extends ZlibDecoder {
2828

29+
private static final int MIN_OUTPUT_BUFFER_SIZE = 512;
2930
private final Inflater z = new Inflater();
3031
private byte[] dictionary;
3132
private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
@@ -167,7 +168,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
167168

168169
try {
169170
loop: for (;;) {
170-
decompressed = prepareDecompressBuffer(ctx, decompressed, z.avail_in << 1);
171+
decompressed = prepareDecompressBuffer(
172+
ctx, decompressed, Math.max(z.avail_in << 1, MIN_OUTPUT_BUFFER_SIZE));
171173
z.avail_out = decompressed.writableBytes();
172174
z.next_out = decompressed.array();
173175
z.next_out_index = decompressed.arrayOffset() + decompressed.writerIndex();
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/JZlibTest.java
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
*/
1616
package io.netty.handler.codec.compression;
1717

18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import io.netty.channel.embedded.EmbeddedChannel;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.CsvSource;
23+
24+
import java.io.ByteArrayInputStream;
25+
import java.io.ByteArrayOutputStream;
26+
import java.util.Arrays;
27+
import java.util.zip.Deflater;
28+
import java.util.zip.Inflater;
29+
import java.util.zip.InflaterInputStream;
30+
31+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
32+
1833
public class JZlibTest extends ZlibTest {
1934

2035
@Override
@@ -26,4 +41,61 @@ protected ZlibEncoder createEncoder(ZlibWrapper wrapper) {
2641
protected ZlibDecoder createDecoder(ZlibWrapper wrapper, int maxAllocation) {
2742
return new JZlibDecoder(wrapper, maxAllocation);
2843
}
44+
45+
@ParameterizedTest
46+
@CsvSource({ "65537, 2147483647", "33333, 7" })
47+
public void testHighlyCompressibleRawStreamIsFullyDecoded(int size, int chunkSize) throws Exception {
48+
byte[] data = new byte[size];
49+
Arrays.fill(data, (byte) 'a');
50+
byte[] compressed = rawDeflate(data);
51+
52+
// Verify the fixture independently before using it to judge JZlibDecoder.
53+
assertArrayEquals(data, jdkInflate(compressed));
54+
55+
EmbeddedChannel channel = new EmbeddedChannel(createDecoder(ZlibWrapper.NONE));
56+
ByteArrayOutputStream decoded = new ByteArrayOutputStream();
57+
try {
58+
for (int offset = 0; offset < compressed.length; offset += chunkSize) {
59+
int length = Math.min(chunkSize, compressed.length - offset);
60+
channel.writeInbound(Unpooled.wrappedBuffer(compressed, offset, length).copy());
61+
}
62+
channel.finish();
63+
64+
ByteBuf message;
65+
while ((message = channel.readInbound()) != null) {
66+
message.readBytes(decoded, message.readableBytes());
67+
message.release();
68+
}
69+
assertArrayEquals(data, decoded.toByteArray());
70+
} finally {
71+
decoded.close();
72+
channel.close();
73+
}
74+
}
75+
76+
private static byte[] rawDeflate(byte[] data) {
77+
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
78+
deflater.setInput(data);
79+
deflater.finish();
80+
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
81+
byte[] buffer = new byte[8192];
82+
while (!deflater.finished()) {
83+
compressed.write(buffer, 0, deflater.deflate(buffer));
84+
}
85+
deflater.end();
86+
return compressed.toByteArray();
87+
}
88+
89+
private static byte[] jdkInflate(byte[] compressed) throws Exception {
90+
ByteArrayOutputStream decoded = new ByteArrayOutputStream();
91+
try (InflaterInputStream input = new InflaterInputStream(
92+
new ByteArrayInputStream(compressed), new Inflater(true))) {
93+
byte[] buffer = new byte[8192];
94+
int length;
95+
while ((length = input.read(buffer)) != -1) {
96+
decoded.write(buffer, 0, length);
97+
}
98+
}
99+
return decoded.toByteArray();
100+
}
29101
}

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.