Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions Sources/FoundationEssentials/Data/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ internal final class __DataStorage : @unchecked Sendable {

@inlinable // This is @inlinable as trivially computable.
var mutableBytes: UnsafeMutableRawPointer? {
return _bytes?.advanced(by: -_offset)
return _bytes?.advanced(by: _offset &* -1) // _offset is guaranteed to be non-negative, so it can never overflow when negating
}

@inlinable
Expand Down Expand Up @@ -955,7 +955,8 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
@inlinable // This is @inlinable as trivially computable.
var count: Int {
get {
return Int(slice.upperBound - slice.lowerBound)
// 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
return Int(slice.upperBound &- slice.lowerBound)
}
set(newValue) {
assert(newValue < HalfInt.max)
Expand Down Expand Up @@ -1112,7 +1113,8 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect

@inlinable @inline(__always) // This is @inlinable as trivially computable.
var count: Int {
return range.upperBound - range.lowerBound
// 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
return range.upperBound &- range.lowerBound
}

@inlinable @inline(__always) // This is @inlinable as a trivial initializer.
Expand Down Expand Up @@ -2214,10 +2216,10 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
switch _representation {
case .empty:
buffer = UnsafeRawBufferPointer(start: nil, count: 0)
case .inline:
case .inline(let inline):
buffer = unsafe UnsafeRawBufferPointer(
start: UnsafeRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
count: inline.count
)
case .large(let slice):
buffer = unsafe UnsafeRawBufferPointer(
Expand Down Expand Up @@ -2252,10 +2254,10 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
switch _representation {
case .empty:
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
case .inline:
case .inline(let inline):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
count: inline.count
)
case .large(var slice):
// Clear _representation during the unique check to avoid double counting the reference, and assign the mutated slice back to _representation afterwards
Expand Down Expand Up @@ -2293,10 +2295,10 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
switch _representation {
case .empty:
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
case .inline:
case .inline(let inline):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
count: inline.count
)
case .large(var slice):
// Clear _representation during the unique check to avoid double counting the reference, and assign the mutated slice back to _representation afterwards
Expand Down