Skip to content

Commit

Permalink
Fixes infinite recursion when casting AnySerializable to wrong type
Browse files Browse the repository at this point in the history
If a values associated type `Serializable` , equals the values type, `toValue` would infinitely call itself.

The test `testWrongCast` reproduces this and I fixed it by comparing the `nextType` with the current type.
  • Loading branch information
leoMehlig committed Sep 2, 2023
1 parent bd14dae commit 403672a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Defaults/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ extension Defaults.Serializable {
return anyObject
}

guard let nextType = T.Serializable.self as? any Defaults.Serializable.Type else {
guard let nextType = T.Serializable.self as? any Defaults.Serializable.Type, nextType != T.self else {
// This is a special case for the types which do not conform to `Defaults.Serializable` (for example, `Any`).
return T.bridge.deserialize(anyObject as? T.Serializable) as? T
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/DefaultsTests/DefaultsAnySeriliazableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,10 @@ final class DefaultsAnySerializableTests: XCTestCase {

waitForExpectations(timeout: 10)
}

func testWrongCast() {
let value = Defaults.AnySerializable(false)
XCTAssertEqual(value.get(Bool.self), false)

Check warning on line 472 in Tests/DefaultsTests/DefaultsAnySeriliazableTests.swift

View workflow job for this annotation

GitHub Actions / lint

XCTest Specific Matcher Violation: Prefer the specific matcher 'XCTAssertFalse' instead (xct_specific_matcher)
XCTAssertNil(value.get(String.self))
}
}

0 comments on commit 403672a

Please sign in to comment.