Skip to content

Commit

Permalink
Fix a bug where xlen larger than 0x7fff was rejected (#1334)
Browse files Browse the repository at this point in the history
Backported from 81bce1a
  • Loading branch information
xstefank authored Oct 1, 2023
1 parent e68262c commit b4fa875
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion okio/src/main/java/okio/GzipSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void consumeHeader() throws IOException {
if (((flags >> FEXTRA) & 1) == 1) {
source.require(2);
if (fhcrc) updateCrc(source.buffer(), 0, 2);
int xlen = source.buffer().readShortLe();
int xlen = source.buffer().readShortLe() & 0xffff;
source.require(xlen);
if (fhcrc) updateCrc(source.buffer(), 0, xlen);
source.skip(xlen);
Expand Down
16 changes: 15 additions & 1 deletion okio/src/test/java/okio/GzipSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package okio;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import org.junit.Test;

import static okio.Util.UTF_8;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -182,6 +184,18 @@ private void assertGzipped(Buffer gzipped) throws IOException {
}
}

@Test public void extraLongXlen() throws Exception {
int xlen = 0xffff;
Buffer gzippedSource = new Buffer()
.write(gzipHeaderWithFlags((byte) 0x04));
gzippedSource.writeShort((short) xlen);
gzippedSource.write(new byte[xlen]);
gzippedSource.write(ByteString.decodeHex("f3c8540400dac59e7903000000"));

Buffer gunzipped = gunzip(gzippedSource);
assertEquals("Hi!", gunzipped.readUtf8());
}

private ByteString gzipHeaderWithFlags(byte flags) {
byte[] result = gzipHeader.toByteArray();
result[3] = flags;
Expand Down

0 comments on commit b4fa875

Please sign in to comment.