Skip to content

Commit d18b2c3

Browse files
parkerannabeyang
andauthored
Fixed an infinite loop caused by force casting NSError to CocoaError. (#5115) (#5119)
(cherry picked from commit bf7369a) Co-authored-by: noriaki watanabe <[email protected]>
1 parent 36bdaf7 commit d18b2c3

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Sources/Foundation/NSError.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ extension CocoaError: _ObjectiveCBridgeable {
704704
}
705705

706706
public static func _forceBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) {
707-
result = _unconditionallyBridgeFromObjectiveC(x)
707+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
708+
fatalError("Unable to bridge \(NSError.self) to \(self)")
709+
}
708710
}
709711

710712
public static func _conditionallyBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) -> Bool {

Tests/Foundation/TestNSError.swift

+6
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,10 @@ class TestCocoaError: XCTestCase {
222222
XCTAssertNotNil(e.underlying as? POSIXError)
223223
XCTAssertEqual(e.underlying as? POSIXError, POSIXError.init(.EACCES))
224224
}
225+
226+
func test_forceCast() {
227+
let nsError = NSError(domain: NSCocoaErrorDomain, code: CocoaError.coderInvalidValue.rawValue)
228+
let error = nsError as! CocoaError
229+
XCTAssertEqual(error.errorCode, CocoaError.coderInvalidValue.rawValue)
230+
}
225231
}

0 commit comments

Comments
 (0)