Skip to content

Commit

Permalink
Fix a bug where xlen larger than 0x7fff was rejected (square#1280)
Browse files Browse the repository at this point in the history
We treated this short value as unsigned and it should have been
treated as signed.
  • Loading branch information
squarejesse authored Jun 22, 2023
1 parent b9b3fce commit 81bce1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion okio/src/jvmMain/kotlin/okio/GzipSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class GzipSource(source: Source) : Source {
if (flags.getBit(FEXTRA)) {
source.require(2)
if (fhcrc) updateCrc(source.buffer, 0, 2)
val xlen = source.buffer.readShortLe().toLong()
val xlen = (source.buffer.readShortLe().toInt() and 0xffff).toLong()
source.require(xlen)
if (fhcrc) updateCrc(source.buffer, 0, xlen)
source.skip(xlen)
Expand Down
22 changes: 18 additions & 4 deletions okio/src/jvmTest/kotlin/okio/GzipKotlinTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ import org.junit.Test
class GzipKotlinTest {
@Test fun sink() {
val data = Buffer()
val gzip = (data as Sink).gzip()
gzip.buffer().writeUtf8("Hi!").close()
(data as Sink).gzip().buffer().use { gzip ->
gzip.writeUtf8("Hi!")
}
assertEquals("1f8b0800000000000000f3c8540400dac59e7903000000", data.readByteString().hex())
}

@Test fun source() {
val buffer = Buffer().write("1f8b0800000000000000f3c8540400dac59e7903000000".decodeHex())
val gzip = (buffer as Source).gzip()
assertEquals("Hi!", gzip.buffer().readUtf8())
(buffer as Source).gzip().buffer().use { gzip ->
assertEquals("Hi!", gzip.readUtf8())
}
}

@Test fun extraLongXlen() {
val xlen = 0xffff
val buffer = Buffer()
.write("1f8b0804000000000000".decodeHex())
.writeShort(xlen)
.write(ByteArray(xlen))
.write("f3c8540400dac59e7903000000".decodeHex())
(buffer as Source).gzip().buffer().use { gzip ->
assertEquals("Hi!", gzip.readUtf8())
}
}
}

0 comments on commit 81bce1a

Please sign in to comment.