From 81bce1a30af244550b0324597720e4799281da7b Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Thu, 22 Jun 2023 09:22:45 -0400 Subject: [PATCH] Fix a bug where xlen larger than 0x7fff was rejected (#1280) We treated this short value as unsigned and it should have been treated as signed. --- okio/src/jvmMain/kotlin/okio/GzipSource.kt | 2 +- .../src/jvmTest/kotlin/okio/GzipKotlinTest.kt | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/okio/src/jvmMain/kotlin/okio/GzipSource.kt b/okio/src/jvmMain/kotlin/okio/GzipSource.kt index ff1e3d323a..1cc4172aad 100644 --- a/okio/src/jvmMain/kotlin/okio/GzipSource.kt +++ b/okio/src/jvmMain/kotlin/okio/GzipSource.kt @@ -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) diff --git a/okio/src/jvmTest/kotlin/okio/GzipKotlinTest.kt b/okio/src/jvmTest/kotlin/okio/GzipKotlinTest.kt index e6b407bcc5..dfe7182a55 100644 --- a/okio/src/jvmTest/kotlin/okio/GzipKotlinTest.kt +++ b/okio/src/jvmTest/kotlin/okio/GzipKotlinTest.kt @@ -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()) + } } }