Skip to content

Commit b4fa875

Browse files
authored
Fix a bug where xlen larger than 0x7fff was rejected (#1334)
Backported from 81bce1a
1 parent e68262c commit b4fa875

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

okio/src/main/java/okio/GzipSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private void consumeHeader() throws IOException {
127127
if (((flags >> FEXTRA) & 1) == 1) {
128128
source.require(2);
129129
if (fhcrc) updateCrc(source.buffer(), 0, 2);
130-
int xlen = source.buffer().readShortLe();
130+
int xlen = source.buffer().readShortLe() & 0xffff;
131131
source.require(xlen);
132132
if (fhcrc) updateCrc(source.buffer(), 0, xlen);
133133
source.skip(xlen);

okio/src/test/java/okio/GzipSourceTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package okio;
1717

18+
import org.junit.Test;
19+
20+
import java.io.ByteArrayOutputStream;
1821
import java.io.IOException;
1922
import java.util.zip.CRC32;
20-
import org.junit.Test;
2123

2224
import static okio.Util.UTF_8;
2325
import static org.junit.Assert.assertEquals;
@@ -182,6 +184,18 @@ private void assertGzipped(Buffer gzipped) throws IOException {
182184
}
183185
}
184186

187+
@Test public void extraLongXlen() throws Exception {
188+
int xlen = 0xffff;
189+
Buffer gzippedSource = new Buffer()
190+
.write(gzipHeaderWithFlags((byte) 0x04));
191+
gzippedSource.writeShort((short) xlen);
192+
gzippedSource.write(new byte[xlen]);
193+
gzippedSource.write(ByteString.decodeHex("f3c8540400dac59e7903000000"));
194+
195+
Buffer gunzipped = gunzip(gzippedSource);
196+
assertEquals("Hi!", gunzipped.readUtf8());
197+
}
198+
185199
private ByteString gzipHeaderWithFlags(byte flags) {
186200
byte[] result = gzipHeader.toByteArray();
187201
result[3] = flags;

0 commit comments

Comments
 (0)