From c5e0589751a97ae1af938631023e0c87cc15c767 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Wed, 12 May 2021 16:21:53 +1000 Subject: [PATCH] Don't crash on unexpected deserialization error --- .../session/libsession/messaging/jobs/Job.kt | 4 ++-- .../messaging/jobs/MessageSendJob.kt | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/Job.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/Job.kt index b680734bb8..332eca002d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/Job.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/Job.kt @@ -21,7 +21,7 @@ interface Job { fun getFactoryKey(): String interface Factory { - - fun create(data: Data): T + + fun create(data: Data): T? } } \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt index 48266764a7..26620b3057 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt @@ -104,17 +104,29 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job { class Factory : Job.Factory { - override fun create(data: Data): MessageSendJob { + override fun create(data: Data): MessageSendJob? { val serializedMessage = data.getByteArray(MESSAGE_KEY) val serializedDestination = data.getByteArray(DESTINATION_KEY) val kryo = Kryo() // Message val messageInput = Input(serializedMessage) - val message = kryo.readClassAndObject(messageInput) as Message + val message: Message + try { + message = kryo.readClassAndObject(messageInput) as Message + } catch (e: Exception) { + Log.e("Loki", "Couldn't deserialize message send job.", e) + return null + } messageInput.close() // Destination val destinationInput = Input(serializedDestination) - val destination = kryo.readClassAndObject(destinationInput) as Destination + val destination: Destination + try { + destination = kryo.readClassAndObject(destinationInput) as Destination + } catch (e: Exception) { + Log.e("Loki", "Couldn't deserialize message send job.", e) + return null + } destinationInput.close() // Return return MessageSendJob(message, destination)