swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.3k stars 1.14k forks source link

[SR-9101] Bug in NSCalendar/Calendar compare(_:,to:,toGranularity:) under Linux environment #3602

Closed swift-ci closed 5 years ago

swift-ci commented 6 years ago
Previous ID SR-9101
Radar None
Original Reporter danielemm (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment Official Swift 4.2 Docker Image on docker site. Linux environment.
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | Foundation | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: cc3af55a9216fcc3471fbea453fb69d3

Issue Description:

Hi,

I'm Daniele and I'm the author of SwiftDate on github; while I'm working porting the lib under Linux I had found a strange issue using compare with granularity function of Calendar instance.

The following code:

let d2 = Date.init(timeIntervalSince1970: 1529431200.0) // 2018-06-19 18:00:00 +0000
let d3 = Date.init(timeIntervalSince1970: 1529604000.0) // 2018-06-21 18:00:00 +0000
let res = Calendar.current.compare(d2, to: d3, toGranularity: .month)

res should be `.orderedSame (as on macOS/iOS/watchOS/tvOS) but tested on Swift 4.2 compiler under Linux (via docker image) it just return `.orderedAscending`.

spevans commented 6 years ago

It seems that in the last few snapshot releases, this bug has become a bit more crashy:

$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-01-a-ubuntu18.04/usr/bin/swift sr_9101.swift
orderedAscending

$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift sr_9101.swift
Stack dump:
0.  Program arguments: /home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift -frontend -interpret sr_9101.swift -disable-objc-interop -color-diagnostics -module-name sr_9101 
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x4211104]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x420ee8e]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x42112c2]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7fae10bf0890]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so(CFLocaleGetIdentifier+0x0)[0x7fae0616c320]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so(+0x7ced84)[0x7fae06156d84]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so(+0x7d10a0)[0x7fae061590a0]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so($s10Foundation10NSCalendarC10components_4fromAA14DateComponentsVAC4UnitV_AA0E0VtF+0x3a4)[0x7fae05e8fb14]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so($s10Foundation10NSCalendarC7compare_2to0D15UnitGranularityAA16ComparisonResultOAA4DateV_AjC0E0VtF+0x594)[0x7fae05e92304]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so(+0x2e332f)[0x7fae05c6b32f]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so(+0x2e3374)[0x7fae05c6b374]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so($s10Foundation14_MutableHandleC3mapyqd__qd__xKXEKlF+0x4a)[0x7fae05c4eb9a]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/lib/swift/linux/libFoundation.so($s10Foundation8CalendarV7compare_2to0D11GranularityAA16ComparisonResultOAA4DateV_AjC9ComponentOtF+0xa2)[0x7fae05c65192]
[0x7fae110170a5]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0xc673b9]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0xc6b470]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x4eb300]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x4c1b63]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x4bd399]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x46e2b0]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7fae0f036b97]
/home/spse/swift-DEVELOPMENT-SNAPSHOT-2018-11-15-a-ubuntu18.04/usr/bin/swift[0x46c6ca]
Segmentation fault (core dumped)

cc @millenomi this crash might be related to your recent Calendar changes in https://github.com/apple/swift-corelibs-foundation/pull/1755

ffried commented 5 years ago

I've run into the same problem. The following code is enough to cause a crash:

Calendar.current.dateComponents([.year], from: Date())
spevans commented 5 years ago

This has been fixed in master

$ cat sr-9101.swift 
import Foundation

let d2 = Date.init(timeIntervalSince1970: 1529431200.0) // 2018-06-19 18:00:00 +0000
let d3 = Date.init(timeIntervalSince1970: 1529604000.0) // 2018-06-21 18:00:00 +0000
let res = Calendar.current.compare(d2, to: d3, toGranularity: .month)
print(res)
print(res == .orderedSame)

$ ~/swift-DEVELOPMENT-SNAPSHOT-2019-04-16-a-ubuntu18.04/usr/bin/swift sr-9101.swift 
orderedSame
true