Skip to content

NSDateComponents.hash: Fix arithmetic overflows #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 84 additions & 39 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1276,49 +1276,94 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
public override init() {
super.init()
}

open override var hash: Int {
var calHash = 0
if let cal = calendar {
calHash = cal.hashValue
}
if let tz = timeZone {
calHash ^= tz.hashValue
}
var y = year
if NSDateComponentUndefined == y {
y = 0
}
var m = month
if NSDateComponentUndefined == m {
m = 0
}
var d = day
if NSDateComponentUndefined == d {
d = 0
}
var h = hour
if NSDateComponentUndefined == h {
h = 0
}
var mm = minute
if NSDateComponentUndefined == mm {
mm = 0
}
var s = second
if NSDateComponentUndefined == s {
s = 0
}
var yy = yearForWeekOfYear
if NSDateComponentUndefined == yy {
yy = 0
}
return calHash + (32832013 * (y + yy) + 2678437 * m + 86413 * d + 3607 * h + 61 * mm + s) + (41 * weekOfYear + 11 * weekOfMonth + 7 * weekday + 3 * weekdayOrdinal + quarter) * (1 << 5)
var hasher = Hasher()
var mask = 0
// The list of fields fed to the hasher here must be exactly
// the same as the ones compared in isEqual(_:) (modulo
// ordering).
//
// Given that NSDateComponents instances usually only have a
// few fields present, it makes sense to only hash those, as
// an optimization. We keep track of the fields hashed in the
// mask value, which we also feed to the hasher to make sure
// any two unequal values produce different hash encodings.
//
// FIXME: Why not just feed _values, calendar & timeZone to
// the hasher?
if let calendar = calendar {
hasher.combine(calendar)
mask |= 1 << 0
}
if let timeZone = timeZone {
hasher.combine(timeZone)
mask |= 1 << 1
}
if era != NSDateComponentUndefined {
hasher.combine(era)
mask |= 1 << 2
}
if year != NSDateComponentUndefined {
hasher.combine(year)
mask |= 1 << 3
}
if quarter != NSDateComponentUndefined {
hasher.combine(quarter)
mask |= 1 << 4
}
if month != NSDateComponentUndefined {
hasher.combine(month)
mask |= 1 << 5
}
if day != NSDateComponentUndefined {
hasher.combine(day)
mask |= 1 << 6
}
if hour != NSDateComponentUndefined {
hasher.combine(hour)
mask |= 1 << 7
}
if minute != NSDateComponentUndefined {
hasher.combine(minute)
mask |= 1 << 8
}
if second != NSDateComponentUndefined {
hasher.combine(second)
mask |= 1 << 9
}
if nanosecond != NSDateComponentUndefined {
hasher.combine(nanosecond)
mask |= 1 << 10
}
if weekOfYear != NSDateComponentUndefined {
hasher.combine(weekOfYear)
mask |= 1 << 11
}
if weekOfMonth != NSDateComponentUndefined {
hasher.combine(weekOfMonth)
mask |= 1 << 12
}
if yearForWeekOfYear != NSDateComponentUndefined {
hasher.combine(yearForWeekOfYear)
mask |= 1 << 13
}
if weekday != NSDateComponentUndefined {
hasher.combine(weekday)
mask |= 1 << 14
}
if weekdayOrdinal != NSDateComponentUndefined {
hasher.combine(weekdayOrdinal)
mask |= 1 << 15
}
hasher.combine(isLeapMonth)
hasher.combine(mask)
return hasher.finalize()
}

open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? NSDateComponents else { return false }

// FIXME: Why not just compare _values, calendar & timeZone?
return self === other
|| (era == other.era
&& year == other.year
Expand Down
16 changes: 16 additions & 0 deletions TestFoundation/TestCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,26 @@ class TestNSDateComponents: XCTestCase {

static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
return [
("test_hash", test_hash),
("test_copyNSDateComponents", test_copyNSDateComponents),
]
}

func test_hash() {
let c1 = NSDateComponents()
c1.year = 2018
c1.month = 8
c1.day = 1

let c2 = NSDateComponents()
c2.year = 2018
c2.month = 8
c2.day = 1

XCTAssertEqual(c1, c2)
XCTAssertEqual(c1.hash, c2.hash)
}

func test_copyNSDateComponents() {
let components = NSDateComponents()
components.year = 1987
Expand Down