From 9148d274f080cb6d0cf61cbcd20cc1b348702c1a Mon Sep 17 00:00:00 2001 From: Khalid Nasralla Date: Sun, 5 Jan 2025 01:23:41 +0100 Subject: [PATCH] PAINTROID-760 Crash: Buffer underflow --- Paintroid/build.gradle | 2 +- .../serialization/CommandSerializer.kt | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Paintroid/build.gradle b/Paintroid/build.gradle index d049eb3f79..a095510ff1 100644 --- a/Paintroid/build.gradle +++ b/Paintroid/build.gradle @@ -125,7 +125,7 @@ dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' implementation 'androidx.exifinterface:exifinterface:1.3.2' - implementation 'com.esotericsoftware:kryo:5.5.0' + implementation 'com.esotericsoftware:kryo:5.6.2' implementation 'id.zelory:compressor:2.1.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializer.kt b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializer.kt index 13a0df7452..d4a4028984 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializer.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializer.kt @@ -32,8 +32,10 @@ import android.net.Uri import android.os.Build import android.os.Environment import android.provider.MediaStore +import android.util.Log import com.esotericsoftware.kryo.Kryo import com.esotericsoftware.kryo.io.Input +import com.esotericsoftware.kryo.io.KryoBufferUnderflowException import com.esotericsoftware.kryo.io.Output import org.catrobat.paintroid.colorpicker.ColorHistory import org.catrobat.paintroid.command.Command @@ -229,24 +231,26 @@ open class CommandSerializer(private val activityContext: Context, private val c fun readFromInternalMemory(stream: FileInputStream): WorkspaceReturnValue { var commandModel: CommandManagerModel? = null var colorHistory: ColorHistory? = null - - Input(stream).use { input -> - if (!input.readString().equals(MAGIC_VALUE)) { - throw NotCatrobatImageException("Magic Value doesn't exist.") - } - val imageVersion = input.readInt() - if (CURRENT_IMAGE_VERSION != imageVersion) { - setRegisterMapVersion(imageVersion) - registerClasses() - } - commandModel = kryo.readObject(input, CommandManagerModel::class.java) - if (input.available() != 0) { - colorHistory = kryo.readObject(input, ColorHistory::class.java) + try { + Input(stream).use { input -> + if (!input.readString().equals(MAGIC_VALUE)) { + throw NotCatrobatImageException("Magic Value doesn't exist.") + } + val imageVersion = input.readInt() + if (CURRENT_IMAGE_VERSION != imageVersion) { + setRegisterMapVersion(imageVersion) + registerClasses() + } + commandModel = kryo.readObject(input, CommandManagerModel::class.java) + if (input.available() != 0) { + colorHistory = kryo.readObject(input, ColorHistory::class.java) + } } + commandModel?.commands?.reverse() + } catch (e: KryoBufferUnderflowException) { + Log.e("readFromInternalMemory", "Buffer underflow: ${e.message}", e) + return WorkspaceReturnValue(null, null) } - - commandModel?.commands?.reverse() - return WorkspaceReturnValue(commandModel, colorHistory) }