Skip to content

Commit 03399ef

Browse files
author
Daiki Matsudate
authored
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.
1 parent 30b30ef commit 03399ef

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Sources/Crypto/ASN1/ASN1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ extension ASN1 {
438438
for shift in (0..<(lengthBytesNeeded - 1)).reversed() {
439439
// Shift and mask the integer.
440440
self.serializedBytes.formIndex(after: &writeIndex)
441-
self.serializedBytes[writeIndex] = UInt8((contentLength >> (shift * 8)) | 0x80 )
441+
self.serializedBytes[writeIndex] = UInt8(truncatingIfNeeded: (contentLength >> (shift * 8)) | 0x80 )
442442
}
443443

444444
assert(writeIndex == self.serializedBytes.index(lengthIndex, offsetBy: lengthBytesNeeded - 1))

0 commit comments

Comments
 (0)