Skip to content

Commit 0a30fcd

Browse files
authored
ASAN error in _CalendarGregorian calendar implementation (#1347)
Use proper overflow add function. Resolves 144178080
1 parent 84270c1 commit 0a30fcd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,15 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
23092309

23102310
case .month:
23112311
var dc = dateComponents(monthBasedComponents, from: dateInWholeSecond, in: timeZone)
2312-
dc.month = (dc.month ?? 0) + amount
2312+
if let month = dc.month {
2313+
let (res, overflow) = month.addingReportingOverflow(amount)
2314+
guard !overflow else {
2315+
throw .overflow(field, date, nil)
2316+
}
2317+
dc.month = res
2318+
} else {
2319+
dc.month = amount
2320+
}
23132321
capDay(in: &dc) // adding 1 month to Jan 31 should return Feb 29, not Feb 31
23142322
resultInWholeSeconds = try self.date(from: dc, inTimeZone: timeZone, dstRepeatedTimePolicy: .latter)
23152323
case .quarter:

Tests/FoundationInternationalizationTests/CalendarTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,14 @@ final class CalendarTests : XCTestCase {
12321232
let added = calendar.date(byAdding: components, to: date)
12331233
XCTAssertNil(added)
12341234
}
1235+
1236+
do {
1237+
let date = Date(timeIntervalSinceReferenceDate: 1710419015.233922)
1238+
let calendar = Calendar(identifier: .gregorian)
1239+
let value = 9223372036854775806
1240+
let added = calendar.date(byAdding: .month, value: value, to: date)
1241+
XCTAssertNil(added)
1242+
}
12351243
}
12361244

12371245
func test_dateComponentsFromDateOverflow() {

0 commit comments

Comments
 (0)