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 — Auto-port 5.0: Fix JdkZlibDecompressor losing the tail of highly comp… · netty/netty@2b38263

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecompressor.java
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public final class JdkZlibDecompressor extends InputBufferingDecompressor {
3838
private static final int FCOMMENT = 0x10;
3939
private static final int FRESERVED = 0xE0;
4040

41+
/**
42+
* Smallest output buffer we hand to {@link Inflater#inflate(byte[], int, int)}. The number of remaining input
43+
* bytes is only a hint for how much output to expect: the inflater may still hold decoded data that did not fit
44+
* into the previous output buffer, and by then it may have consumed all input bytes already
45+
* ({@link Inflater#getRemaining()} == 0). Inflating into a zero-sized buffer can never make progress.
46+
*/
47+
private static final int MIN_OUTPUT_BUFFER_SIZE = 512;
48+
4149
private Inflater inflater;
4250
private final int maxAllocation;
4351
private final byte[] dictionary;
@@ -69,6 +77,12 @@ private enum GzipState {
6977
* buffer too much (e.g. compact it).
7078
*/
7179
private boolean inputBufferInInflater;
80+
/**
81+
* If this is true, the last {@link Inflater#inflate(byte[], int, int)} filled the output buffer completely, so
82+
* the inflater may still hold decoded data even if {@link Inflater#needsInput()} reports that all input was
83+
* consumed.
84+
*/
85+
private boolean inflaterHasPendingOutput;
7286

7387
JdkZlibDecompressor(Builder builder, ByteBufAllocator allocator) {
7488
super(allocator);
@@ -102,6 +116,8 @@ private enum GzipState {
102116
public Status status() throws DecompressionException {
103117
if (finished) {
104118
return Status.COMPLETE;
119+
} else if (inflaterHasPendingOutput) {
120+
return Status.NEED_OUTPUT;
105121
} else if (inflater == null || inflater.needsInput() || gzipState == GzipState.FOOTER_START) {
106122
return Status.NEED_INPUT;
107123
} else {
@@ -187,7 +203,7 @@ private void consumeInput(ByteBuf buf) {
187203

188204
@Override
189205
ByteBuf processOutput(ByteBuf buf) throws DecompressionException {
190-
int proposedCapacity = inflater.getRemaining() << 1;
206+
int proposedCapacity = Math.max(inflater.getRemaining() << 1, MIN_OUTPUT_BUFFER_SIZE);
191207
int targetCapacity = maxAllocation == 0
192208
? proposedCapacity : Math.min(maxAllocation, proposedCapacity);
193209
ByteBuf decompressed = allocator.heapBuffer(targetCapacity);
@@ -196,9 +212,10 @@ ByteBuf processOutput(ByteBuf buf) throws DecompressionException {
196212
byte[] outArray = decompressed.array();
197213
int writerIndex = decompressed.writerIndex();
198214
int outIndex = decompressed.arrayOffset() + writerIndex;
215+
int writableBytes = decompressed.writableBytes();
199216
int outputLength;
200217
try {
201-
outputLength = inflater.inflate(outArray, outIndex, decompressed.writableBytes());
218+
outputLength = inflater.inflate(outArray, outIndex, writableBytes);
202219
} catch (DataFormatException e) {
203220
throw new DecompressionException("decompression failure", e);
204221
}
@@ -226,6 +243,10 @@ ByteBuf processOutput(ByteBuf buf) throws DecompressionException {
226243
processInput(buf);
227244
}
228245
}
246+
// If we filled the whole buffer the inflater may still have decoded data left that it could not write.
247+
// In that case we must ask for another output buffer, as needsInput() only tells us that all input
248+
// bytes were consumed, not that all output was produced.
249+
inflaterHasPendingOutput = outputLength == writableBytes && !inflater.finished();
229250
success = true;
230251
return decompressed;
231252
} finally {
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibDecompressorTest.java
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
import java.io.IOException;
3131
import java.util.Arrays;
3232
import java.util.List;
33+
import java.util.zip.Deflater;
3334
import java.util.zip.DeflaterOutputStream;
3435
import java.util.zip.GZIPInputStream;
3536
import java.util.zip.GZIPOutputStream;
37+
import java.util.zip.Inflater;
38+
import java.util.zip.InflaterInputStream;
3639

3740
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3841
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -158,6 +161,18 @@ public void testDirectInputRetainedAcrossOutputChunks() throws Exception {
158161
}
159162
}
160163

164+
@Test
165+
public void testHighlyCompressibleStreamIsFullyDecompressed() throws Exception {
166+
// The tail of such a stream expands far beyond the number of remaining input bytes: the inflater pulls the
167+
// last input bytes into its internal state while it still has to emit data. getRemaining() is 0 by then,
168+
// so sizing the output buffer by it alone leaves the inflater without room to write the rest.
169+
byte[] expected = new byte[100000];
170+
Arrays.fill(expected, (byte) 'a');
171+
byte[] compressed = compress(expected);
172+
assertArrayEquals(expected, jdkDecompress(compressed));
173+
assertArrayEquals(expected, decompress(createDecompressor().build(ByteBufAllocator.DEFAULT), compressed));
174+
}
175+
161176
@Test
162177
public void testBadSecondGzipMagicRejected() throws Exception {
163178
assumeTrue(wrapper == ZlibWrapper.GZIP);
@@ -230,6 +245,58 @@ private static byte[] decompress(Decompressor decompressor, byte[] compressed) t
230245
}
231246
}
232247

248+
/**
249+
* Compress with the wrapper this test instance runs with, using the JDK so that the input is known-good
250+
* independently of netty's own encoders.
251+
*/
252+
private byte[] compress(byte[] data) throws IOException {
253+
switch (wrapper) {
254+
case GZIP:
255+
return gzip(data);
256+
case ZLIB:
257+
return deflate(data);
258+
case NONE:
259+
return rawDeflate(data);
260+
default:
261+
throw new AssertionError("Unexpected wrapper: " + wrapper);
262+
}
263+
}
264+
265+
/** Decompress with the JDK, to prove the compressed input is valid. */
266+
private byte[] jdkDecompress(byte[] compressed) throws IOException {
267+
if (wrapper == ZlibWrapper.GZIP) {
268+
return jdkGunzip(compressed);
269+
}
270+
Inflater inflater = new Inflater(wrapper == ZlibWrapper.NONE);
271+
try {
272+
InflaterInputStream input = new InflaterInputStream(new ByteArrayInputStream(compressed), inflater);
273+
ByteArrayOutputStream output = new ByteArrayOutputStream();
274+
byte[] buffer = new byte[256];
275+
int read;
276+
while ((read = input.read(buffer)) != -1) {
277+
output.write(buffer, 0, read);
278+
}
279+
input.close();
280+
output.close();
281+
return output.toByteArray();
282+
} finally {
283+
inflater.end();
284+
}
285+
}
286+
287+
private static byte[] rawDeflate(byte[] data) throws IOException {
288+
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
289+
try {
290+
ByteArrayOutputStream output = new ByteArrayOutputStream();
291+
DeflaterOutputStream stream = new DeflaterOutputStream(output, deflater);
292+
stream.write(data);
293+
stream.close();
294+
return output.toByteArray();
295+
} finally {
296+
deflater.end();
297+
}
298+
}
299+
233300
private static byte[] gzip(byte[] data) throws IOException {
234301
ByteArrayOutputStream output = new ByteArrayOutputStream();
235302
GZIPOutputStream gzip = new GZIPOutputStream(output);

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.