From e620665dda9eb5cc39f09e6087ea4f60a3cbe150 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani <46041660+null2264@users.noreply.github.com> Date: Sun, 30 Jun 2024 20:57:29 +0700 Subject: [PATCH] Add safeguard to prevent ArchiveInputStream from being closed twice (#967) * fix: Add safeguard to prevent ArchiveInputStream from being closed twice * detekt * lint: Make detekt happy --------- Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com> --- .../mihon/core/common/archive/ArchiveInputStream.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/common/src/main/kotlin/mihon/core/common/archive/ArchiveInputStream.kt b/core/common/src/main/kotlin/mihon/core/common/archive/ArchiveInputStream.kt index a9bb87879..1499867c8 100644 --- a/core/common/src/main/kotlin/mihon/core/common/archive/ArchiveInputStream.kt +++ b/core/common/src/main/kotlin/mihon/core/common/archive/ArchiveInputStream.kt @@ -5,8 +5,14 @@ import me.zhanghai.android.libarchive.ArchiveEntry import me.zhanghai.android.libarchive.ArchiveException import java.io.InputStream import java.nio.ByteBuffer +import kotlin.concurrent.Volatile class ArchiveInputStream(buffer: Long, size: Long) : InputStream() { + private val lock = Any() + + @Volatile + private var isClosed = false + private val archive = Archive.readNew() init { @@ -41,6 +47,11 @@ class ArchiveInputStream(buffer: Long, size: Long) : InputStream() { } override fun close() { + synchronized(lock) { + if (isClosed) return + isClosed = true + } + Archive.readFree(archive) }