From 03399ef4651d6dd0e0db515485b514a5db2564ec Mon Sep 17 00:00:00 2001 From: Daiki Matsudate Date: Fri, 17 Jun 2022 15:44:20 +0900 Subject: [PATCH] truncating for big contentLength (#119) When contentLength is greater than 8 bits, i.e., greater than UInt8.max, the original implementation crashes. To work around this, use UInt8.init(truncatingIfNeeded:) to truncate only the lower 8 bits. --- Sources/Crypto/ASN1/ASN1.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Crypto/ASN1/ASN1.swift b/Sources/Crypto/ASN1/ASN1.swift index b9c31182..23428009 100644 --- a/Sources/Crypto/ASN1/ASN1.swift +++ b/Sources/Crypto/ASN1/ASN1.swift @@ -438,7 +438,7 @@ extension ASN1 { for shift in (0..<(lengthBytesNeeded - 1)).reversed() { // Shift and mask the integer. self.serializedBytes.formIndex(after: &writeIndex) - self.serializedBytes[writeIndex] = UInt8((contentLength >> (shift * 8)) | 0x80 ) + self.serializedBytes[writeIndex] = UInt8(truncatingIfNeeded: (contentLength >> (shift * 8)) | 0x80 ) } assert(writeIndex == self.serializedBytes.index(lengthIndex, offsetBy: lengthBytesNeeded - 1))