From 8a3d11e596f83b691099071de41155acedc3234f Mon Sep 17 00:00:00 2001 From: nnabeyang Date: Fri, 18 Oct 2024 08:06:50 +0900 Subject: [PATCH] Fixed an infinite loop caused by force casting NSError to CocoaError. --- Sources/Foundation/NSError.swift | 4 +++- Tests/Foundation/TestNSError.swift | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Foundation/NSError.swift b/Sources/Foundation/NSError.swift index 6f21d3a03d..908d565a95 100644 --- a/Sources/Foundation/NSError.swift +++ b/Sources/Foundation/NSError.swift @@ -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 { diff --git a/Tests/Foundation/TestNSError.swift b/Tests/Foundation/TestNSError.swift index d597fb0a99..5521895d3a 100644 --- a/Tests/Foundation/TestNSError.swift +++ b/Tests/Foundation/TestNSError.swift @@ -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) + } }