Skip to content

Commit b651278

Browse files
authored
Fix a crash on Scalar init with negative numbers (#38829)
1 parent 437302f commit b651278

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

stdlib/public/core/UnicodeScalar.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ extension Unicode.Scalar {
337337
/// }
338338
@inlinable
339339
public init?(_ v: Int) {
340-
if let us = Unicode.Scalar(UInt32(v)) {
341-
self = us
340+
if let exact = UInt32(exactly: v) {
341+
self.init(exact)
342342
} else {
343343
return nil
344344
}

validation-test/stdlib/Unicode.swift.gyb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ UnicodeScalarTests.test("init") {
210210
expectEqual("h", UnicodeScalar(UInt16(104)))
211211
expectEqual("i", UnicodeScalar(UInt8(105)))
212212
expectEqual(nil, UnicodeScalar(UInt32(0xD800)))
213+
expectEqual("j", Unicode.Scalar(Int(0x6A)))
214+
expectEqual(nil, Unicode.Scalar(-0x6A))
213215
}
214216

215217
var UTF8Decoder = TestSuite("UTF8Decoder")

0 commit comments

Comments
 (0)