You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/FoundationEssentials/Data/Data.swift
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -207,7 +207,7 @@ internal final class __DataStorage : @unchecked Sendable {
207
207
208
208
@inlinable // This is @inlinable as trivially computable.
209
209
varmutableBytes:UnsafeMutableRawPointer?{
210
-
return _bytes?.advanced(by:-_offset)
210
+
return _bytes?.advanced(by: _offset&*-1) // _offset is guaranteed to be non-negative, so it can never overflow when negating
211
211
}
212
212
213
213
@inlinable
@@ -955,7 +955,8 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
955
955
@inlinable // This is @inlinable as trivially computable.
956
956
varcount:Int{
957
957
get{
958
-
returnInt(slice.upperBound - slice.lowerBound)
958
+
// The upper bound is guaranteed to be greater than or equal to the lower bound, and the lower bound must be non-negative so subtraction can never overflow
959
+
returnInt(slice.upperBound &- slice.lowerBound)
959
960
}
960
961
set(newValue){
961
962
assert(newValue <HalfInt.max)
@@ -1112,7 +1113,8 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
1112
1113
1113
1114
@inlinable@inline(__always) // This is @inlinable as trivially computable.
1114
1115
varcount:Int{
1115
-
return range.upperBound - range.lowerBound
1116
+
// The upper bound is guaranteed to be greater than or equal to the lower bound, and the lower bound must be non-negative so subtraction can never overflow
1117
+
return range.upperBound &- range.lowerBound
1116
1118
}
1117
1119
1118
1120
@inlinable@inline(__always) // This is @inlinable as a trivial initializer.
@@ -2214,10 +2216,10 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
// Clear _representation during the unique check to avoid double counting the reference, and assign the mutated slice back to _representation afterwards
@@ -2293,10 +2295,10 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
// Clear _representation during the unique check to avoid double counting the reference, and assign the mutated slice back to _representation afterwards
0 commit comments