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 — Only fail in ZlibDecoder when the decompression buffer is full (#17692) · lltopk/netty@e6fc471

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
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@
2626
import io.netty.util.internal.EmptyArrays;
2727
import org.junit.jupiter.api.Test;
2828
import org.junit.jupiter.api.function.Executable;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.CsvSource;
31+
import org.junit.jupiter.params.provider.ValueSource;
2932

3033
import java.io.ByteArrayOutputStream;
3134
import java.io.IOException;
3235
import java.io.OutputStream;
36+
import java.util.Arrays;
3337
import java.util.Random;
3438
import java.util.concurrent.ThreadLocalRandom;
3539
import java.util.zip.DeflaterOutputStream;
3640
import java.util.zip.GZIPInputStream;
3741
import java.util.zip.GZIPOutputStream;
3842

43+
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3945
import static org.junit.jupiter.api.Assertions.assertEquals;
4046
import static org.junit.jupiter.api.Assertions.assertFalse;
4147
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -428,6 +434,78 @@ public void execute() throws Throwable {
428434
assertFalse(chDecoder.finish());
429435
}
430436

437+
@ParameterizedTest
438+
@CsvSource({ "ZLIB, 2", "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512", "GZIP, 512", "NONE, 512" })
439+
public void testMaxAllocationWithInputSplitAcrossReads(ZlibWrapper wrapper, int maxAllocation) {
440+
// The whole output is smaller than maxAllocation, so no read can fill the buffer.
441+
byte[] data = Arrays.copyOf(BYTES_LARGE2, Math.min(maxAllocation - 1, BYTES_LARGE2.length));
442+
byte[] compressed = compress(wrapper, data);
443+
444+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length))
445+
.isEqualTo(data);
446+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length / 2 + 1))
447+
.isEqualTo(data);
448+
assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, 1))
449+
.isEqualTo(data);
450+
}
451+
452+
@ParameterizedTest
453+
@ValueSource(ints = { 600, 1000, 1024 })
454+
public void testMaxAllocationWithIncompressibleInputInOneRead(int size) {
455+
// Random bytes do not compress, so the input is about as long as the output.
456+
byte[] data = Arrays.copyOf(BYTES_LARGE, size);
457+
byte[] compressed = compress(ZlibWrapper.ZLIB, data);
458+
459+
assertThat(decodeInChunks(createDecoder(ZlibWrapper.ZLIB, 1024), compressed, compressed.length))
460+
.isEqualTo(data);
461+
}
462+
463+
@ParameterizedTest
464+
@CsvSource({ "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512" })
465+
public void testMaxAllocationStillLimitsOutputOfOneRead(ZlibWrapper wrapper, int maxAllocation) {
466+
// A small input whose output is far larger than maxAllocation is still rejected.
467+
byte[] compressed = compress(wrapper, new byte[65536]);
468+
EmbeddedChannel channel = new EmbeddedChannel(createDecoder(wrapper, maxAllocation));
469+
470+
assertThatThrownBy(() -> channel.writeInbound(Unpooled.wrappedBuffer(compressed)))
471+
.isInstanceOf(DecompressionException.class)
472+
.hasMessageStartingWith("Decompression buffer has reached maximum size");
473+
channel.finishAndReleaseAll();
474+
}
475+
476+
private byte[] compress(ZlibWrapper wrapper, byte[] data) {
477+
EmbeddedChannel channel = new EmbeddedChannel(createEncoder(wrapper));
478+
channel.writeOutbound(Unpooled.wrappedBuffer(data));
479+
channel.finish();
480+
return readAll(channel, false);
481+
}
482+
483+
private static byte[] decodeInChunks(ZlibDecoder decoder, byte[] compressed, int chunkSize) {
484+
EmbeddedChannel channel = new EmbeddedChannel(decoder);
485+
for (int offset = 0; offset < compressed.length; offset += chunkSize) {
486+
int length = Math.min(chunkSize, compressed.length - offset);
487+
channel.writeInbound(Unpooled.wrappedBuffer(compressed, offset, length));
488+
}
489+
channel.finish();
490+
return readAll(channel, true);
491+
}
492+
493+
private static byte[] readAll(EmbeddedChannel channel, boolean inbound) {
494+
ByteBuf all = Unpooled.buffer();
495+
for (;;) {
496+
ByteBuf buf = inbound ? channel.<ByteBuf>readInbound() : channel.<ByteBuf>readOutbound();
497+
if (buf == null) {
498+
break;
499+
}
500+
all.writeBytes(buf);
501+
buf.release();
502+
}
503+
byte[] bytes = new byte[all.readableBytes()];
504+
all.readBytes(bytes);
505+
all.release();
506+
return bytes;
507+
}
508+
431509
private static byte[] gzip(byte[] bytes) throws IOException {
432510
ByteArrayOutputStream out = new ByteArrayOutputStream();
433511
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.