From 9c806cdc6383788d758ac4406509f24287a79db1 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 18 Nov 2024 11:27:10 -0800 Subject: [PATCH] remove another force unwrap --- Sources/XMTPiOS/EncodedContentCompression.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/XMTPiOS/EncodedContentCompression.swift b/Sources/XMTPiOS/EncodedContentCompression.swift index 0e824d20..5203ee0b 100644 --- a/Sources/XMTPiOS/EncodedContentCompression.swift +++ b/Sources/XMTPiOS/EncodedContentCompression.swift @@ -28,14 +28,18 @@ public enum EncodedContentCompression { _ data: Data, using algorithm: compression_algorithm ) -> Data? { let destinationBuffer = UnsafeMutablePointer.allocate( - capacity: data.count) + capacity: data.count + ) defer { destinationBuffer.deallocate() } - let compressedSize = data.withUnsafeBytes { sourceBuffer in - compression_encode_buffer( + let compressedSize = data.withUnsafeBytes { sourceBuffer -> Int in + guard let sourcePointer = sourceBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { + return 0 // Return 0 to indicate failure + } + return compression_encode_buffer( destinationBuffer, data.count, - sourceBuffer.baseAddress!.assumingMemoryBound(to: UInt8.self), - data.count, nil, algorithm) + sourcePointer, data.count, nil, algorithm + ) } guard compressedSize > 0 else { return nil }