mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-21 10:42:25 +08:00
Fix potential memory leak with struct ByteReadPacket
This commit is contained in:
parent
0f54e2aa27
commit
d4ee81c79a
@ -20,6 +20,7 @@ import kotlinx.serialization.modules.SerializersModule
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.tars.internal.TarsDecoder
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.tars.internal.TarsInput
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.tars.internal.TarsOld
|
||||
import net.mamoe.mirai.utils.read
|
||||
|
||||
/**
|
||||
* The main entry point to work with Tars serialization.
|
||||
@ -32,7 +33,9 @@ internal class Tars(
|
||||
private val old = TarsOld(charset)
|
||||
|
||||
fun <T> dumpTo(serializer: SerializationStrategy<T>, ojb: T, output: Output) {
|
||||
output.writePacket(old.dumpAsPacket(serializer, ojb))
|
||||
old.dumpAsPacket(serializer, ojb).use {
|
||||
output.writePacket(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> load(deserializer: DeserializationStrategy<T>, input: Input): T {
|
||||
@ -44,7 +47,9 @@ internal class Tars(
|
||||
}
|
||||
|
||||
override fun <T> decodeFromByteArray(deserializer: DeserializationStrategy<T>, bytes: ByteArray): T {
|
||||
return load(deserializer, ByteReadPacket(bytes))
|
||||
bytes.read {
|
||||
return load(deserializer, this)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -52,7 +52,9 @@ internal fun <T : JceStruct> ByteReadPacket.readJceStruct(
|
||||
serializer: DeserializationStrategy<T>,
|
||||
length: Int = this.remaining.toInt()
|
||||
): T {
|
||||
return Tars.UTF_8.load(serializer, this.readPacketExact(length))
|
||||
this.readPacketExact(length).use {
|
||||
return Tars.UTF_8.load(serializer, it)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <T : JceStruct> BytePacketBuilder.writeJceRequestPacket(
|
||||
|
Loading…
Reference in New Issue
Block a user