Reject unsupported library snapshot versions
This commit is contained in:
@@ -45,6 +45,10 @@ object LibrarySnapshotCodec {
|
||||
if (!magic.contentEquals(MAGIC_BYTES)) {
|
||||
throw IOException("Invalid snapshot magic")
|
||||
}
|
||||
return Header(String(magic, Charsets.US_ASCII), dataInput.readInt())
|
||||
val schemaVersion = dataInput.readInt()
|
||||
if (schemaVersion != SCHEMA_VERSION) {
|
||||
throw IOException("Unsupported snapshot schema version: $schemaVersion")
|
||||
}
|
||||
return Header(String(magic, Charsets.US_ASCII), schemaVersion)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,4 +48,16 @@ class LibrarySnapshotCodecTest {
|
||||
LibrarySnapshotCodec.readHeader(ByteArrayInputStream(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readHeader_unknownSchemaVersion_throwsIOException() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeHeader(output)
|
||||
val bytes = output.toByteArray()
|
||||
bytes[bytes.lastIndex] = 2.toByte()
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readHeader(ByteArrayInputStream(bytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user