Skip to content

Commit

Permalink
Pedantic exception handling in init and close
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Apr 17, 2024
1 parent 6a139e3 commit f5ce70a
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions gateway/src/nativeMain/kotlin/Inflater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ internal actual fun Inflater(): Inflater = object : Inflater {
private var decompressed = UByteArray(1024) // buffer only grows, is reused for every zlib inflate call
private var decompressedLen = 0
private var closed = false
private val zStream = nativeHeap.alloc<z_stream>()

private val zStream = nativeHeap.alloc<z_stream>().also { zStream ->
// next_in, avail_in, zalloc, zfree and opaque must be initialized before calling inflateInit
zStream.next_in = null
zStream.avail_in = 0u
zStream.zalloc = null
zStream.zfree = null
zStream.opaque = null
// initialize msg just in case, we use it for throwing exceptions
zStream.msg = null
val ret = inflateInit(zStream.ptr)
if (ret != Z_OK) {
init {
try {
// next_in, avail_in, zalloc, zfree and opaque must be initialized before calling inflateInit
zStream.next_in = null
zStream.avail_in = 0u
zStream.zalloc = null
zStream.zfree = null
zStream.opaque = null
// initialize msg to null in case inflateInit doesn't, we use it for throwing exceptions
zStream.msg = null
val ret = inflateInit(zStream.ptr)
if (ret != Z_OK) throw ZlibException(zStream.msg, ret)
} catch (e: Throwable) {
try {
throw ZlibException(zStream.msg, ret)
} finally {
nativeHeap.free(zStream)
} catch (freeException: Throwable) {
e.addSuppressed(freeException)
}
throw e
}
}

Expand Down Expand Up @@ -66,11 +70,17 @@ internal actual fun Inflater(): Inflater = object : Inflater {
override fun close() {
if (closed) return
closed = true
val ret = inflateEnd(zStream.ptr)
try {
val ret = inflateEnd(zStream.ptr)
if (ret != Z_OK) throw ZlibException(zStream.msg, ret)
} finally {
nativeHeap.free(zStream)
} catch (e: Throwable) {
try {
nativeHeap.free(zStream)
} catch (freeException: Throwable) {
e.addSuppressed(freeException)
}
throw e
}
nativeHeap.free(zStream)
}
}

0 comments on commit f5ce70a

Please sign in to comment.