Skip to content

Commit c1f021c

Browse files
committed
slices of slices that use range expressions incorrectly calculated relative indexing
1 parent 37927d3 commit c1f021c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Foundation/Data.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ public final class _DataStorage {
351351

352352
// fast-path for appending directly from another data storage
353353
@inline(__always)
354-
public func append(_ otherData: _DataStorage, startingAt start: Int) {
354+
public func append(_ otherData: _DataStorage, startingAt start: Int, endingAt end: Int) {
355355
let otherLength = otherData.length
356356
if otherLength == 0 { return }
357357
if let bytes = otherData.bytes {
358-
append(bytes.advanced(by: start), length: otherLength)
358+
append(bytes.advanced(by: start), length: end - start)
359359
}
360360
}
361361

@@ -1132,7 +1132,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
11321132

11331133
@_versioned
11341134
internal func _validateRange<R: RangeExpression>(_ range: R) where R.Bound == Int {
1135-
let r = range.relative(to: 0..<R.Bound.max)
1135+
let lower = R.Bound(_sliceRange.lowerBound)
1136+
let upper = R.Bound(_sliceRange.upperBound)
1137+
let r = range.relative(to: lower..<upper)
11361138
precondition(r.lowerBound >= _sliceRange.lowerBound && r.lowerBound <= _sliceRange.upperBound, "Range \(r) is out of bounds of range \(_sliceRange)")
11371139
precondition(r.upperBound >= _sliceRange.lowerBound && r.upperBound <= _sliceRange.upperBound, "Range \(r) is out of bounds of range \(_sliceRange)")
11381140
}
@@ -1328,7 +1330,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
13281330
if !isKnownUniquelyReferenced(&_backing) {
13291331
_backing = _backing.mutableCopy(_sliceRange)
13301332
}
1331-
_backing.append(other._backing, startingAt: other._sliceRange.lowerBound)
1333+
_backing.append(other._backing, startingAt: other._sliceRange.lowerBound, endingAt: other._sliceRange.upperBound)
13321334
_sliceRange = _sliceRange.lowerBound..<(_sliceRange.upperBound + other.count)
13331335
}
13341336

@@ -1598,7 +1600,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15981600
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
15991601
@inline(__always)
16001602
get {
1601-
let range = rangeExpression.relative(to: 0..<R.Bound.max)
1603+
let lower = R.Bound(_sliceRange.lowerBound)
1604+
let upper = R.Bound(_sliceRange.upperBound)
1605+
let range = rangeExpression.relative(to: lower..<upper)
16021606
let start: Int = numericCast(range.lowerBound)
16031607
let end: Int = numericCast(range.upperBound)
16041608
let r: Range<Int> = start..<end
@@ -1607,7 +1611,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
16071611
}
16081612
@inline(__always)
16091613
set {
1610-
let range = rangeExpression.relative(to: 0..<R.Bound.max)
1614+
let lower = R.Bound(_sliceRange.lowerBound)
1615+
let upper = R.Bound(_sliceRange.upperBound)
1616+
let range = rangeExpression.relative(to: lower..<upper)
16111617
let start: Int = numericCast(range.lowerBound)
16121618
let end: Int = numericCast(range.upperBound)
16131619
let r: Range<Int> = start..<end

0 commit comments

Comments
 (0)