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 — Port: Only fail in ZlibDecoder when the decompression buffer is full … · netty/netty@8b401cb

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ protected ByteBuf prepareDecompressBuffer(ChannelHandlerContext ctx, ByteBuf buf
6969
}
7070

7171
// this always expands the buffer if possible, even if the expansion is less than preferredSize
72-
// we throw the exception only if the buffer could not be expanded at all
72+
// we throw the exception only if the buffer could not be expanded at all and has no space left
7373
// this means that one final attempt to deserialize will always be made with the buffer at maxAllocation
74-
if (buffer.ensureWritable(preferredSize, true) == 1) {
74+
if (buffer.ensureWritable(preferredSize, true) == 1 && !buffer.isWritable()) {
7575
// buffer must be consumed so subclasses don't add it to output
7676
// we therefore duplicate it when calling decompressionBufferExhausted() to guarantee non-interference
7777
// but wait until after to consume it so the subclass can tell how much output is really in the buffer
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZlibTest.java
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,23 @@
2525
import io.netty.util.ReferenceCountUtil;
2626
import io.netty.util.internal.EmptyArrays;
2727
import org.junit.jupiter.api.Test;
28+
import org.assertj.core.api.ThrowableAssert;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.CsvSource;
31+
import org.junit.jupiter.params.provider.ValueSource;
2832

2933
import java.io.ByteArrayOutputStream;
3034
import java.io.IOException;
3135
import java.io.OutputStream;
36+
import java.util.Arrays;
3237
import java.util.Random;
3338
import java.util.concurrent.ThreadLocalRandom;
3439
import java.util.zip.DeflaterOutputStream;
3540
import java.util.zip.GZIPInputStream;
3641
import java.util.zip.GZIPOutputStream;
3742

43+
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3845
import static org.junit.jupiter.api.Assertions.assertEquals;
3946
import static org.junit.jupiter.api.Assertions.assertFalse;
4047
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -408,6 +415,83 @@ public void testMaxAllocation() throws Exception {
408415
assertFalse(chDecoder.finish());
409416
}
410417

418+
@ParameterizedTest
419+
@CsvSource({ "ZLIB, 2", "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512", "GZIP, 512", "NONE, 512" })
420+
public void testMaxAllocationWithInputSplitAcrossReads(ZlibWrapper wrapper, int maxAllocation) throws Exception {
421+
// The whole output is smaller than maxAllocation, so no read can fill the buffer.
422+
byte[] data = Arrays.copyOf(BYTES_LARGE2, Math.min(maxAllocation - 1, BYTES_LARGE2.length));
423+
byte[] compressed = compress(wrapper, data);
424+
425+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length))
426+
.isEqualTo(data);
427+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length / 2 + 1))
428+
.isEqualTo(data);
429+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, 1))
430+
.isEqualTo(data);
431+
}
432+
433+
@ParameterizedTest
434+
@ValueSource(ints = { 600, 1000, 1024 })
435+
public void testMaxAllocationWithIncompressibleInputInOneRead(int size) throws Exception {
436+
// Random bytes do not compress, so the input is about as long as the output.
437+
byte[] data = Arrays.copyOf(BYTES_LARGE, size);
438+
byte[] compressed = compress(ZlibWrapper.ZLIB, data);
439+
440+
assertThat(decodeInChunks(createDecoder(ZlibWrapper.ZLIB, 1024), compressed, compressed.length))
441+
.isEqualTo(data);
442+
}
443+
444+
@ParameterizedTest
445+
@CsvSource({ "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512" })
446+
public void testMaxAllocationStillLimitsOutputOfOneRead(ZlibWrapper wrapper, int maxAllocation) throws Exception {
447+
// A small input whose output is far larger than maxAllocation is still rejected.
448+
final byte[] compressed = compress(wrapper, new byte[65536]);
449+
final EmbeddedChannel channel = new EmbeddedChannel(createDecoder(wrapper, maxAllocation));
450+
451+
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
452+
@Override
453+
public void call() throws Throwable {
454+
channel.writeInbound(Unpooled.wrappedBuffer(compressed));
455+
}
456+
})
457+
.isInstanceOf(DecompressionException.class)
458+
.hasMessageStartingWith("Decompression buffer has reached maximum size");
459+
channel.finishAndReleaseAll();
460+
}
461+
462+
private byte[] compress(ZlibWrapper wrapper, byte[] data) throws Exception {
463+
EmbeddedChannel channel = new EmbeddedChannel(createEncoder(wrapper));
464+
channel.writeOutbound(Unpooled.wrappedBuffer(data));
465+
channel.finish();
466+
return readAll(channel, false);
467+
}
468+
469+
private static byte[] decodeInChunks(ZlibDecoder decoder, byte[] compressed, int chunkSize) throws Exception {
470+
EmbeddedChannel channel = new EmbeddedChannel(decoder);
471+
for (int offset = 0; offset < compressed.length; offset += chunkSize) {
472+
int length = Math.min(chunkSize, compressed.length - offset);
473+
channel.writeInbound(Unpooled.wrappedBuffer(compressed, offset, length));
474+
}
475+
channel.finish();
476+
return readAll(channel, true);
477+
}
478+
479+
private static byte[] readAll(EmbeddedChannel channel, boolean inbound) {
480+
ByteBuf all = Unpooled.buffer();
481+
for (;;) {
482+
ByteBuf buf = inbound ? channel.<ByteBuf>readInbound() : channel.<ByteBuf>readOutbound();
483+
if (buf == null) {
484+
break;
485+
}
486+
all.writeBytes(buf);
487+
buf.release();
488+
}
489+
byte[] bytes = new byte[all.readableBytes()];
490+
all.readBytes(bytes);
491+
all.release();
492+
return bytes;
493+
}
494+
411495
private static byte[] gzip(byte[] bytes) throws IOException {
412496
ByteArrayOutputStream out = new ByteArrayOutputStream();
413497
GZIPOutputStream stream = new GZIPOutputStream(out);

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.