GeezerVison is showing github.com — Fix ZstdDecoder and ZstdDecompressor with a non-positive defaultMaxFo… · netty/netty@5baf6f3
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/compression test/java/io/netty/handler/codec/compression
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java +14-3Lines changed: 14 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,9 @@ public final class ZstdDecoder extends ByteToMessageDecoder { | 41 | 41 | } | 42 | 42 |
| 43 | 43 | private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | | 44 | + // Output buffer size while maxForwardBytes is zero or negative: such a threshold forwards every buffer that has | | 45 | + // content, but cannot size one. ZstdDecompressor uses it too. | | 46 | + static final int DEFAULT_OUTPUT_BUFFER_SIZE = 64 * 1024; | 44 | 47 | /** | 45 | 48 | * Default maximum size of a single output buffer, in bytes (4 MiB). | 46 | 49 | */ | @@ -53,6 +56,7 @@ public final class ZstdDecoder extends ByteToMessageDecoder { | 53 | 56 | private static final int MIN_WINDOW_LOG = 10; | 54 | 57 | private static final int MAX_WINDOW_LOG = 31; | 55 | 58 | private final int maxForwardBytes; | | 59 | + private final int outputBufferSize; | 56 | 60 | private final int maxWindowLog; | 57 | 61 | private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream(); | 58 | 62 | private ZstdInputStreamNoFinalizer zstdIs; | @@ -103,8 +107,14 @@ public ZstdDecoder(int maximumAllocationSize) { | 103 | 107 | * decoder may allocate per stream. | 104 | 108 | */ | 105 | 109 | public ZstdDecoder(int maximumAllocationSize, int maxWindowLog) { | | 110 | + this(maximumAllocationSize, maxWindowLog, DEFAULT_MAX_FORWARD_BYTES); | | 111 | + } | | 112 | + | | 113 | + // Package-private for testing, as the default is read from a system property once per JVM. | | 114 | + ZstdDecoder(int maximumAllocationSize, int maxWindowLog, int defaultMaxForwardBytes) { | 106 | 115 | ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize"); | 107 | | - this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : DEFAULT_MAX_FORWARD_BYTES; | | 116 | + this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : defaultMaxForwardBytes; | | 117 | + this.outputBufferSize = maxForwardBytes > 0 ? maxForwardBytes : DEFAULT_OUTPUT_BUFFER_SIZE; | 108 | 118 | this.maxWindowLog = ObjectUtil.checkInRange(maxWindowLog, MIN_WINDOW_LOG, MAX_WINDOW_LOG, "maxWindowLog"); | 109 | 119 | } | 110 | 120 |
| @@ -142,12 +152,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 142 | 152 | if (outBuffer == null) { | 143 | 153 | // The frame content size is read from the (untrusted) frame header, so never use it | 144 | 154 | // to size the buffer beyond what we forward in one go. | 145 | | - outBuffer = ctx.alloc().heapBuffer((int) Math.min(maxForwardBytes, uncompressedLength)); | | 155 | + outBuffer = ctx.alloc().heapBuffer((int) Math.min(outputBufferSize, uncompressedLength)); | 146 | 156 | } | 147 | 157 | do { | 148 | 158 | w = outBuffer.writeBytes(zstdIs, outBuffer.writableBytes()); | 149 | 159 | } while (w > 0 && outBuffer.isWritable()); | 150 | | - if (!outBuffer.isWritable() || outBuffer.readableBytes() >= maxForwardBytes) { | | 160 | + if (outBuffer.isReadable() && | | 161 | + (!outBuffer.isWritable() || outBuffer.readableBytes() >= maxForwardBytes)) { | 151 | 162 | needsRead = false; | 152 | 163 | ctx.fireChannelRead(outBuffer); | 153 | 164 | outBuffer = null; | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecompressor.java +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,18 +41,27 @@ public final class ZstdDecompressor implements Decompressor { | 41 | 41 | private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | 42 | 42 |
| 43 | 43 | private final ByteBufAllocator allocator; | | 44 | + private final int outputBufferSize; | 44 | 45 |
| 45 | 46 | private final MutableByteBufInputStream mutableInput = new MutableByteBufInputStream(); | 46 | 47 | private final ZstdInputStreamNoFinalizer output; | 47 | 48 |
| 48 | 49 | ZstdDecompressor(Builder builder, ByteBufAllocator allocator) { | | 50 | + this(builder, allocator, DEFAULT_MAX_FORWARD_BYTES); | | 51 | + } | | 52 | + | | 53 | + // Package-private for testing, as the default is read from a system property once per JVM. | | 54 | + ZstdDecompressor(Builder builder, ByteBufAllocator allocator, int defaultMaxForwardBytes) { | 49 | 55 | // Don't use static here as we want to still allow to load the classes. | 50 | 56 | try { | 51 | 57 | Zstd.ensureAvailability(); | 52 | 58 | } catch (Throwable throwable) { | 53 | 59 | throw new ExceptionInInitializerError(throwable); | 54 | 60 | } | 55 | 61 | this.allocator = allocator; | | 62 | + // A threshold of zero or less cannot size a buffer. | | 63 | + this.outputBufferSize = defaultMaxForwardBytes > 0 ? | | 64 | + defaultMaxForwardBytes : ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE; | 56 | 65 | ZstdInputStreamNoFinalizer output = null; | 57 | 66 | try { | 58 | 67 | output = new ZstdInputStreamNoFinalizer(mutableInput); | @@ -112,9 +121,9 @@ public void endOfInput() throws DecompressionException { | 112 | 121 |
| 113 | 122 | @Override | 114 | 123 | public ByteBuf takeOutput() throws DecompressionException { | 115 | | - ByteBuf buf = allocator.buffer(DEFAULT_MAX_FORWARD_BYTES, DEFAULT_MAX_FORWARD_BYTES); | | 124 | + ByteBuf buf = allocator.buffer(outputBufferSize, outputBufferSize); | 116 | 125 | try { | 117 | | - buf.writeBytes(output, DEFAULT_MAX_FORWARD_BYTES); | | 126 | + buf.writeBytes(output, outputBufferSize); | 118 | 127 | } catch (IOException e) { | 119 | 128 | buf.release(); | 120 | 129 | throw new DecompressionException(e); | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecoderTest.java +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import io.netty.buffer.Unpooled; | 23 | 23 | import io.netty.channel.embedded.EmbeddedChannel; | 24 | 24 | import org.junit.jupiter.api.Test; | | 25 | +import org.junit.jupiter.params.ParameterizedTest; | | 26 | +import org.junit.jupiter.params.provider.ValueSource; | 25 | 27 |
| 26 | 28 | import java.util.Random; | 27 | 29 |
| @@ -177,6 +179,41 @@ public void testDecompressLargeFrameWhenUnlimited() { | 177 | 179 | } | 178 | 180 | } | 179 | 181 |
| | 182 | + @ParameterizedTest | | 183 | + @ValueSource(ints = {0, -1}) | | 184 | + public void testDecompressWhenUnlimitedWithNonPositiveDefaultMaxForwardBytes(int defaultMaxForwardBytes) { | | 185 | + byte[] payload = new byte[1024 * 1024]; | | 186 | + new Random(12345L).nextBytes(payload); | | 187 | + byte[] compressed = Zstd.compress(payload); | | 188 | + | | 189 | + EmbeddedChannel ch = new EmbeddedChannel( | | 190 | + new ZstdDecoder(0, ZstdDecoder.DEFAULT_MAX_WINDOW_LOG, defaultMaxForwardBytes)); | | 191 | + try { | | 192 | + assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(compressed))); | | 193 | + | | 194 | + ByteBuf acc = Unpooled.buffer(); | | 195 | + try { | | 196 | + ByteBuf b; | | 197 | + while ((b = ch.readInbound()) != null) { | | 198 | + try { | | 199 | + assertTrue(b.isReadable()); | | 200 | + assertTrue(b.readableBytes() <= ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE); | | 201 | + acc.writeBytes(b); | | 202 | + } finally { | | 203 | + b.release(); | | 204 | + } | | 205 | + } | | 206 | + byte[] actual = new byte[acc.readableBytes()]; | | 207 | + acc.readBytes(actual); | | 208 | + assertArrayEquals(payload, actual); | | 209 | + } finally { | | 210 | + acc.release(); | | 211 | + } | | 212 | + } finally { | | 213 | + ch.finishAndReleaseAll(); | | 214 | + } | | 215 | + } | | 216 | + | 180 | 217 | private static byte[] compressWithWindowLog(byte[] data, int windowLog) { | 181 | 218 | try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { | 182 | 219 | ctx.setLevel(Zstd.defaultCompressionLevel()); | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecompressorTest.java +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import io.netty.buffer.Unpooled; | 23 | 23 | import io.netty.channel.ChannelHandler; | 24 | 24 | import org.junit.jupiter.api.Test; | | 25 | +import org.junit.jupiter.params.ParameterizedTest; | | 26 | +import org.junit.jupiter.params.provider.ValueSource; | 25 | 27 |
| 26 | 28 | import java.util.Arrays; | 27 | 29 | import java.util.Random; | | 30 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; | 31 | 33 | import static org.junit.jupiter.api.Assertions.assertFalse; | 32 | 34 | import static org.junit.jupiter.api.Assertions.assertThrows; | | 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; | 33 | 36 |
| 34 | 37 | public class ZstdDecompressorTest extends AbstractDecompressorTest { | 35 | 38 | @Override | @@ -89,6 +92,40 @@ public void testTruncatedFrameIsRejectedAfterEndOfInput() { | 89 | 92 | assertThrows(DecompressionException.class, () -> decompress(truncated, 20)); | 90 | 93 | } | 91 | 94 |
| | 95 | + @ParameterizedTest | | 96 | + @ValueSource(ints = {0, -1}) | | 97 | + public void testDecompressWithNonPositiveDefaultMaxForwardBytes(int defaultMaxForwardBytes) | | 98 | + throws DecompressionException { | | 99 | + byte[] payload = new byte[1024 * 1024]; | | 100 | + new Random(12345L).nextBytes(payload); | | 101 | + byte[] compressed = Zstd.compress(payload); | | 102 | + | | 103 | + ByteBuf output = Unpooled.buffer(); | | 104 | + try (Decompressor decompressor = new DefensiveDecompressor(new ZstdDecompressor( | | 105 | + ZstdDecompressor.builder(), ByteBufAllocator.DEFAULT, defaultMaxForwardBytes))) { | | 106 | + assertEquals(Decompressor.Status.NEED_INPUT, decompressor.status()); | | 107 | + decompressor.addInput(Unpooled.wrappedBuffer(compressed)); | | 108 | + // The body takes 16 outputs of 64 KiB, so the bound only stops a decompressor that makes no progress. | | 109 | + for (int i = 0; i < 1024 && decompressor.status() == Decompressor.Status.NEED_OUTPUT; i++) { | | 110 | + ByteBuf b = decompressor.takeOutput(); | | 111 | + try { | | 112 | + assertTrue(b.readableBytes() <= ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE); | | 113 | + output.writeBytes(b); | | 114 | + } finally { | | 115 | + b.release(); | | 116 | + } | | 117 | + } | | 118 | + assertEquals(Decompressor.Status.NEED_INPUT, decompressor.status()); | | 119 | + decompressor.endOfInput(); | | 120 | + assertEquals(Decompressor.Status.COMPLETE, decompressor.status()); | | 121 | + byte[] actual = new byte[output.readableBytes()]; | | 122 | + output.readBytes(actual); | | 123 | + assertArrayEquals(payload, actual); | | 124 | + } finally { | | 125 | + output.release(); | | 126 | + } | | 127 | + } | | 128 | + | 92 | 129 | private static byte[] decompress(byte[] compressed, int maxWindowLog) throws DecompressionException { | 93 | 130 | ByteBuf input = Unpooled.wrappedBuffer(compressed); | 94 | 131 | ByteBuf output = Unpooled.buffer(); | |
|