Skip to content

Commit 9fa3cec

Browse files
authored
[Foundation] IndexPath should not cause numeric overflow in addition to calculate hash values (swiftlang#9476)
1 parent e0c248c commit 9fa3cec

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

stdlib/public/SDK/Foundation/IndexPath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
658658
let totalBits = MemoryLayout<Int>.size * 8
659659
let lengthBits = 8
660660
let firstIndexBits = (totalBits - lengthBits) / 2
661-
return count + (first << lengthBits) + (last << (lengthBits + firstIndexBits))
661+
return count &+ (first << lengthBits) &+ (last << (lengthBits + firstIndexBits))
662662
}
663663

664664
switch _indexes {

test/stdlib/TestIndexPath.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class TestIndexPath: TestIndexPathSuper {
226226
let ip2: IndexPath = [1, 1, 1]
227227

228228
expectNotEqual(ip1.hashValue, ip2.hashValue)
229+
230+
IndexPath(indexes: [Int.max >> 8, 2, Int.max >> 36]).hashValue // this should not cause an overflow crash
229231
}
230232

231233
func testEquality() {

0 commit comments

Comments
 (0)