Skip to content

Commit d7b0fac

Browse files
committed
Resetting a slice region should expand the slice to the maximum of the region (not a out of bounds index of the backing buffer)
1 parent 08072a4 commit d7b0fac

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,9 +1502,8 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15021502
_backing = _backing.mutableCopy(_sliceRange)
15031503
}
15041504
_backing.resetBytes(in: range)
1505-
if _sliceRange.upperBound < range.location + range.length {
1506-
let newLength = range.location + range.length
1507-
_sliceRange = _sliceRange.lowerBound..<(_sliceRange.lowerBound + newLength)
1505+
if _sliceRange.upperBound < range.upperBound {
1506+
_sliceRange = _sliceRange.lowerBound..<range.upperBound
15081507
}
15091508
}
15101509

test/stdlib/TestData.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3635,6 +3635,12 @@ class TestData : TestDataSuper {
36353635
expectEqual(slice1.hashValue, slice2.hashValue)
36363636
expectEqual(slice2.hashValue, slice3.hashValue)
36373637
}
3638+
3639+
func test_slice_resize_growth() {
3640+
var data = Data(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[4..<9]
3641+
data.resetBytes(in: data.endIndex.advanced(by: -1)..<data.endIndex.advanced(by: 1))
3642+
expectEqual(data, Data(bytes: [4, 5, 6, 7, 0, 0]))
3643+
}
36383644
}
36393645

36403646
#if !FOUNDATION_XCTEST
@@ -3943,7 +3949,7 @@ DataTests.test("test_validateMutation_slice_cow_customMutableBacking_replaceSubr
39433949
DataTests.test("test_validateMutation_slice_cow_customMutableBacking_replaceSubrangeWithCollection") { TestData().test_validateMutation_slice_cow_customMutableBacking_replaceSubrangeWithCollection() }
39443950
DataTests.test("test_validateMutation_slice_cow_customMutableBacking_replaceSubrangeWithBytes") { TestData().test_validateMutation_slice_cow_customMutableBacking_replaceSubrangeWithBytes() }
39453951
DataTests.test("test_sliceHash") { TestData().test_sliceHash() }
3946-
3952+
DataTests.test("test_slice_resize_growth") { TestData().test_slice_resize_growth() }
39473953
// XCTest does not have a crash detection, whereas lit does
39483954
DataTests.test("bounding failure subdata") {
39493955
let data = "Hello World".data(using: .utf8)!

0 commit comments

Comments
 (0)