Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ internal class ByteArrayProtoReader32(
}
STATE_LENGTH_DELIMITED -> {
val length = internalReadVarint32()
if (length < 0) throw ProtocolException("Negative length: $length. Reader position: $pos. Last read tag: $tag.")
skip(length)
}
STATE_VARINT -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ open class ProtoReader(private val source: BufferedSource) {
}
STATE_LENGTH_DELIMITED -> {
val length = internalReadVarint32()
if (length < 0) throw ProtocolException("Negative length: $length. Reader position: $pos. Last read tag: $tag.")
pos += length.toLong()
source.skip(length.toLong())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,15 @@ class ProtoReader32Test {
}.isInstanceOf<IOException>().hasMessage("Wire recursion limit exceeded")
}

/** We had a bug where negative lengths in skipped groups crashed with runtime exceptions. */
@Test fun testSkipGroupRejectsNegativeLength() {
val data = "9b060a80ffffff0f9c06".decodeHex()

assertFailure {
Person.ADAPTER.decode(data)
}.isInstanceOf<IOException>()
.hasMessage("Negative length: -128. Reader position: 8. Last read tag: 1.")
}

// Consider pasting new tests into ProtoReaderTest.kt also.
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,15 @@ class ProtoReaderTest {
}.isInstanceOf<IOException>().hasMessage("Wire recursion limit exceeded")
}

/** We had a bug where negative lengths in skipped groups crashed with runtime exceptions. */
@Test fun testSkipGroupRejectsNegativeLength() {
val data = "9b060a80ffffff0f9c06".decodeHex()

assertFailure {
Person.ADAPTER.decode(Buffer().write(data))
}.isInstanceOf<IOException>()
.hasMessage("Negative length: -128. Reader position: 8. Last read tag: 1.")
}

// Consider pasting new tests into ProtoReader32Test.kt also.
}
Loading