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 — Merge commit from fork · netty/netty@67207c1

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/Lz4FrameDecoder.java
+42-3Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
5252
*/
5353
public class Lz4FrameDecoder extends ByteToMessageDecoder {
54+
private final int maxDecompressedLength;
5455
/**
5556
* Current state of stream.
5657
*/
@@ -117,6 +118,21 @@ public Lz4FrameDecoder(boolean validateChecksums) {
117118
this(LZ4Factory.fastestInstance(), validateChecksums);
118119
}
119120

121+
/**
122+
* Creates a LZ4 decoder with fastest decoder instance available on your machine.
123+
*
124+
* @param validateChecksums if {@code true}, the checksum field will be validated against the actual
125+
* uncompressed data, and if the checksums do not match, a suitable
126+
* {@link DecompressionException} will be thrown
127+
* @param maxDecompressedLength
128+
* maximum length of the decompressed block. If {@code 0} is given it uses {@code 32MB}
129+
* by default.
130+
*/
131+
public Lz4FrameDecoder(boolean validateChecksums, int maxDecompressedLength) {
132+
this(LZ4Factory.fastestInstance(), validateChecksums ? new Lz4XXHash32(DEFAULT_SEED) : null,
133+
maxDecompressedLength);
134+
}
135+
120136
/**
121137
* Creates a new LZ4 decoder with customizable implementation.
122138
*
@@ -143,8 +159,25 @@ public Lz4FrameDecoder(LZ4Factory factory, boolean validateChecksums) {
143159
* You may set {@code null} if you do not want to validate checksum of each block
144160
*/
145161
public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) {
162+
this(factory, checksum, MAX_BLOCK_SIZE);
163+
}
164+
165+
/**
166+
* Creates a new customizable LZ4 decoder.
167+
*
168+
* @param factory user customizable {@link LZ4Factory} instance
169+
* which may be JNI bindings to the original C implementation, a pure Java implementation
170+
* or a Java implementation that uses the {@link sun.misc.Unsafe}
171+
* @param checksum the {@link Checksum} instance to use to check data for integrity.
172+
* You may set {@code null} if you do not want to validate checksum of each block
173+
* @param maxDecompressedLength
174+
* maximum length of the decompressed block. If {@code 0} is given it uses {@code 32MB} by default.
175+
*/
176+
public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum, int maxDecompressedLength) {
146177
decompressor = ObjectUtil.checkNotNull(factory, "factory").fastDecompressor();
147178
this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum);
179+
this.maxDecompressedLength = maxDecompressedLength == 0 ? MAX_BLOCK_SIZE :
180+
ObjectUtil.checkInRange(maxDecompressedLength, 0, MAX_BLOCK_SIZE, "maxDecompressedLength");
148181
}
149182

150183
@Override
@@ -172,12 +205,18 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
172205
}
173206

174207
int decompressedLength = Integer.reverseBytes(in.readInt());
175-
final int maxDecompressedLength = 1 << compressionLevel;
176-
if (decompressedLength < 0 || decompressedLength > maxDecompressedLength) {
208+
if (decompressedLength > maxDecompressedLength) {
177209
throw new DecompressionException(String.format(
178-
"invalid decompressedLength: %d (expected: 0-%d)",
210+
"decompressedLength too large: %d (expected: 0-%d)",
179211
decompressedLength, maxDecompressedLength));
180212
}
213+
214+
final int maxLocalDecompressedLength = 1 << compressionLevel;
215+
if (decompressedLength < 0 || decompressedLength > maxLocalDecompressedLength) {
216+
throw new DecompressionException(String.format(
217+
"invalid decompressedLength: %d (expected: 0-%d)",
218+
decompressedLength, maxLocalDecompressedLength));
219+
}
181220
if (decompressedLength == 0 && compressedLength != 0
182221
|| decompressedLength != 0 && compressedLength == 0
183222
|| blockType == BLOCK_TYPE_NON_COMPRESSED && decompressedLength != compressedLength) {
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public Lz4FrameDecoderTest() throws Exception {
4545

4646
@Override
4747
protected EmbeddedChannel createChannel() {
48-
return new EmbeddedChannel(new Lz4FrameDecoder(true));
48+
// Use max limit of 31 MB as we want to test that we reject 32 MB in one of the tests
49+
return new EmbeddedChannel(new Lz4FrameDecoder(true, 31 * 1024 * 1024));
4950
}
5051

5152
@Test
@@ -90,6 +91,24 @@ public void execute() {
9091
}, "invalid decompressedLength");
9192
}
9293

94+
@Test
95+
public void testTooLargeDecompressedLength() {
96+
final ByteBuf buf = Unpooled.buffer(22, 22);
97+
buf.writeLong(MAGIC_NUMBER);
98+
buf.writeByte(BLOCK_TYPE_COMPRESSED | 0x0F);
99+
buf.writeIntLE(1);
100+
buf.writeIntLE(1 << 25);
101+
buf.writeIntLE(0);
102+
buf.writeByte(0);
103+
104+
assertThrows(DecompressionException.class, new Executable() {
105+
@Override
106+
public void execute() {
107+
channel.writeInbound(buf);
108+
}
109+
});
110+
}
111+
93112
@Test
94113
public void testDecompressedAndCompressedLengthMismatch() {
95114
final byte[] data = Arrays.copyOf(DATA, DATA.length);

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.