Harden library snapshot string decoding
This commit is contained in:
@@ -23,6 +23,8 @@ import java.io.DataOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.CodingErrorAction
|
||||
|
||||
object LibrarySnapshotCodec {
|
||||
const val MAGIC = "AUXSNAP"
|
||||
@@ -70,6 +72,28 @@ object LibrarySnapshotCodec {
|
||||
}
|
||||
val bytes = ByteArray(byteCount)
|
||||
dataInput.readFully(bytes)
|
||||
return String(bytes, Charsets.UTF_8)
|
||||
return Charsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||||
.decode(ByteBuffer.wrap(bytes))
|
||||
.toString()
|
||||
}
|
||||
|
||||
fun writeNullableString(output: OutputStream, value: String?) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeBoolean(value != null)
|
||||
if (value != null) {
|
||||
writeString(dataOutput, value)
|
||||
}
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readNullableString(input: InputStream): String? {
|
||||
val marker = DataInputStream(input).readUnsignedByte()
|
||||
return when (marker) {
|
||||
0 -> null
|
||||
1 -> readString(input)
|
||||
else -> throw IOException("Invalid nullable string marker: $marker")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,11 @@ package org.oxycblt.musikr.snapshot
|
||||
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.EOFException
|
||||
import java.io.IOException
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
|
||||
@@ -84,4 +86,64 @@ class LibrarySnapshotCodecTest {
|
||||
|
||||
assertEquals(value, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readString_invalidLengths_throwIOException() {
|
||||
for (byteCount in listOf(-1, 1024 * 1024 + 1)) {
|
||||
val output = ByteArrayOutputStream()
|
||||
DataOutputStream(output).writeInt(byteCount)
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readString(ByteArrayInputStream(output.toByteArray()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readString_truncatedPayload_throwsEOFException() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeString(output, "truncated")
|
||||
val bytes = output.toByteArray()
|
||||
val truncated = bytes.copyOf(bytes.size - 1)
|
||||
|
||||
assertThrows(EOFException::class.java) {
|
||||
LibrarySnapshotCodec.readString(ByteArrayInputStream(truncated))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readString_malformedUtf8_throwsIOException() {
|
||||
val output = ByteArrayOutputStream()
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeInt(1)
|
||||
dataOutput.writeByte(0x80)
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readString(ByteArrayInputStream(output.toByteArray()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nullableString_roundTrip_distinguishesNullEmptyAndText() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeNullableString(output, null)
|
||||
LibrarySnapshotCodec.writeNullableString(output, "")
|
||||
LibrarySnapshotCodec.writeNullableString(output, "Queue 初音ミク")
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
assertNull(LibrarySnapshotCodec.readNullableString(input))
|
||||
assertEquals("", LibrarySnapshotCodec.readNullableString(input))
|
||||
assertEquals("Queue 初音ミク", LibrarySnapshotCodec.readNullableString(input))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readNullableString_invalidMarker_throwsIOException() {
|
||||
val output = ByteArrayOutputStream()
|
||||
output.write(2)
|
||||
LibrarySnapshotCodec.writeString(output, "still present")
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readNullableString(ByteArrayInputStream(output.toByteArray()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user