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.
This commit is contained in:
Daiki Matsudate authored and GitHub committed 2022-06-16 23:44:20 -07:00
1 parent 30b30ef3a1
commit 03399ef465
1 file changed
+1 -1
+1 -1
View File
@@ -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))