Skip to content

Commit 6cde050

Browse files
authored
Introduce compatibility behavior of Calendar.date(byAdding:value:to:wrappingComponents) (#1206)
Some app is passing in `value: -9223372036854775808` to the function below, and force unwrapping the result. It then crashes when the force unwrap fails. ```swift /// - returns: A new date, or nil if a date could not be calculated with the given input. public func date(byAdding component: Component, value: Int, to date: Date, wrappingComponents: Bool = false) -> Date? ``` This is caused by #1149. Prior to the change, we were using `_CalendarICU`'s implementation, where we truncate the input value to `Int32`, which becomes 0 in this case. That results in us returning the input `date` unchanged. Now with #1149, we truthfully return `nil` because the calculation cannot be done. We could restore the old behavior, but that implementation is incorrect. It's also clearly a wrong assumption on the client side. Add a compatibility check and restore the old behavior if needed. Resolves 145862455
1 parent 3c9559d commit 6cde050

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Sources/FoundationEssentials/Calendar/Calendar.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,14 @@ public struct Calendar : Hashable, Equatable, Sendable {
601601
return nil
602602
}
603603

604-
return self.date(byAdding: dc, to: date.capped, wrappingComponents: wrappingComponents)
604+
let result = self.date(byAdding: dc, to: date.capped, wrappingComponents: wrappingComponents)
605+
#if FOUNDATION_FRAMEWORK
606+
// Compatibility path - we found some apps depending on the result being non-nil
607+
if Calendar.compatibility2 {
608+
return result ?? date
609+
}
610+
#endif
611+
return result
605612
}
606613

607614
/// Returns a sequence of `Date`s, calculated by adding a scaled amount of `Calendar.Component`s to a starting `Date`.

0 commit comments

Comments
 (0)