From c736dd37fb941c5ffa1c0cbd354ea3b2d29621e0 Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Fri, 5 May 2023 15:51:50 -0300 Subject: [PATCH] [#1019] Memo has trailing garbled text (#1023) Changes the way unpadded bytes are turned into a UTF-8 Swift String without using cString assuming APIs that would overflow memory and add garbled trailing bytes. Closes #1019 add changelog entry --- CHANGELOG.md | 6 ++++++ Sources/ZcashLightClientKit/Model/Memo.swift | 17 ++--------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a55594b..7309191f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Unreleased +### [#1019] Memo has trailing garbled text + +Changes the way unpadded bytes are turned into a UTF-8 Swift String +without using cString assuming APIs that would overflow memory and +add garbled trailing bytes. + ### Changed - `WalletTransactionEncoder` now uses a `LightWalletService` to submit the encoded transactions. diff --git a/Sources/ZcashLightClientKit/Model/Memo.swift b/Sources/ZcashLightClientKit/Model/Memo.swift index daa8a818..2b15c6f5 100644 --- a/Sources/ZcashLightClientKit/Model/Memo.swift +++ b/Sources/ZcashLightClientKit/Model/Memo.swift @@ -165,8 +165,8 @@ public extension MemoBytes { func intoMemo() throws -> Memo { switch self.bytes[0] { case 0x00 ... 0xF4: - let unpadded = self.unpaddedRawBytes() - guard let validatedUTF8String = String(validatingUTF8: unpadded) else { + let unpaddedBytes = self.unpaddedRawBytes() + guard let validatedUTF8String = String(bytes: unpaddedBytes, encoding: .utf8) else { throw ZcashError.memoBytesInvalidUTF8 } @@ -218,19 +218,6 @@ extension Array where Element == UInt8 { } } -extension String { - public init?(validatingUTF8 cString: UnsafePointer) { - guard let (str, _) = String.decodeCString( - cString, - as: UTF8.self, - repairingInvalidCodeUnits: false - ) else { - return nil - } - self = str - } -} - extension Optional where WrappedType == String { func intoMemo() throws -> Memo { switch self {