Skip to content

Commit

Permalink
Fixed an infinite loop caused by force casting NSError to CocoaError. (
Browse files Browse the repository at this point in the history
  • Loading branch information
nnabeyang authored Oct 18, 2024
1 parent cb6b2fa commit bf7369a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,9 @@ extension CocoaError: _ObjectiveCBridgeable {
}

public static func _forceBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) {
result = _unconditionallyBridgeFromObjectiveC(x)
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(NSError.self) to \(self)")
}
}

public static func _conditionallyBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) -> Bool {
Expand Down
6 changes: 6 additions & 0 deletions Tests/Foundation/TestNSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,10 @@ class TestCocoaError: XCTestCase {
XCTAssertNotNil(e.underlying as? POSIXError)
XCTAssertEqual(e.underlying as? POSIXError, POSIXError.init(.EACCES))
}

func test_forceCast() {
let nsError = NSError(domain: NSCocoaErrorDomain, code: CocoaError.coderInvalidValue.rawValue)
let error = nsError as! CocoaError
XCTAssertEqual(error.errorCode, CocoaError.coderInvalidValue.rawValue)
}
}

0 comments on commit bf7369a

Please sign in to comment.