[#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
This commit is contained in:
Francisco Gindre 2023-05-05 15:51:50 -03:00 committed by GitHub
parent f5e7c027af
commit c736dd37fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 15 deletions

View File

@ -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.

View File

@ -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<UInt8>) {
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 {