Reject unsupported library snapshot versions

This commit is contained in:
jay
2026-11-13 20:54:40 -05:00
parent 32c037f58d
commit 17172705b4
2 changed files with 17 additions and 1 deletions
@@ -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))
}
}
}