Skip to content

Commit 9cf48a8

Browse files
authored
Merge pull request #10499 from phausler/swift-4.0-branch-index_path_slice_init
[Foundation] Prevent incorrect slice access when dropping elements
2 parents 5e5c4f1 + cf61f8c commit 9cf48a8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

stdlib/public/SDK/Foundation/IndexPath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
290290
case 0:
291291
return .empty
292292
case 1:
293-
return .single(slice[0])
293+
return .single(slice.first!)
294294
case 2:
295-
return .pair(slice[0], slice[1])
295+
return .pair(slice.first!, slice.last!)
296296
default:
297297
return .array(Array<Int>(slice))
298298
}

test/stdlib/TestIndexPath.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,14 @@ class TestIndexPath: TestIndexPathSuper {
720720
let slice = indexPath[1..<1]
721721
expectEqual(0, slice.count)
722722
}
723+
724+
func test_dropFirst() {
725+
var pth = IndexPath(indexes:[1,2,3,4])
726+
while !pth.isEmpty {
727+
// this should not crash
728+
pth = pth.dropFirst()
729+
}
730+
}
723731
}
724732

725733
#if !FOUNDATION_XCTEST
@@ -772,5 +780,6 @@ IndexPathTests.test("test_AnyHashableContainingIndexPath") { TestIndexPath().tes
772780
IndexPathTests.test("test_AnyHashableCreatedFromNSIndexPath") { TestIndexPath().test_AnyHashableCreatedFromNSIndexPath() }
773781
IndexPathTests.test("test_unconditionallyBridgeFromObjectiveC") { TestIndexPath().test_unconditionallyBridgeFromObjectiveC() }
774782
IndexPathTests.test("test_slice_1ary") { TestIndexPath().test_slice_1ary() }
783+
IndexPathTests.test("test_dropFirst") { TestIndexPath().test_dropFirst() }
775784
runAllTests()
776785
#endif

0 commit comments

Comments
 (0)