Skip to content

Commit 9ac65cf

Browse files
committed
[stdlib] fix some unsafe errors
1 parent acbdfef commit 9ac65cf

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

stdlib/public/core/Span/MutableRawSpan.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
2727
@usableFromInline
2828
internal let _count: Int
2929

30+
@unsafe
3031
@_alwaysEmitIntoClient
3132
internal func _start() -> UnsafeMutableRawPointer {
3233
unsafe _pointer._unsafelyUnwrappedUnchecked
@@ -171,8 +172,8 @@ extension RawSpan {
171172
@_alwaysEmitIntoClient
172173
@lifetime(borrow mutableRawSpan)
173174
public init(_mutableRawSpan mutableRawSpan: borrowing MutableRawSpan) {
174-
let (start, count) = (mutableRawSpan._start(), mutableRawSpan._count)
175-
let span = unsafe RawSpan(_unsafeStart: start, byteCount: count)
175+
let (start, count) = unsafe (mutableRawSpan._pointer, mutableRawSpan._count)
176+
let span = unsafe RawSpan(_unchecked: start, byteCount: count)
176177
self = unsafe _overrideLifetime(span, borrowing: mutableRawSpan)
177178
}
178179
}
@@ -432,6 +433,10 @@ extension MutableRawSpan {
432433
public mutating func update(
433434
fromContentsOf source: RawSpan
434435
) -> Int {
436+
_precondition(
437+
source.byteCount <= self.byteCount,
438+
"destination span cannot contain every byte from source."
439+
)
435440
if source.byteCount == 0 { return 0 }
436441
unsafe source.withUnsafeBytes {
437442
unsafe _start().copyMemory(from: $0.baseAddress!, byteCount: $0.count)
@@ -560,7 +565,7 @@ extension MutableRawSpan {
560565
@_alwaysEmitIntoClient
561566
@lifetime(&self)
562567
mutating public func extracting(_: UnboundedRange) -> Self {
563-
let newSpan = unsafe Self(_unchecked: _start(), byteCount: _count)
568+
let newSpan = unsafe Self(_unchecked: _pointer, byteCount: _count)
564569
return unsafe _overrideLifetime(newSpan, mutating: &self)
565570
}
566571
}

stdlib/public/core/Span/MutableSpan.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct MutableSpan<Element: ~Copyable>
2828
@usableFromInline
2929
internal let _count: Int
3030

31+
@unsafe
3132
@_alwaysEmitIntoClient
3233
internal func _start() -> UnsafeMutableRawPointer {
3334
unsafe _pointer._unsafelyUnwrappedUnchecked
@@ -447,7 +448,7 @@ extension MutableSpan where Element: BitwiseCopyable {
447448
_ body: (_ buffer: UnsafeMutableRawBufferPointer) throws(E) -> Result
448449
) throws(E) -> Result {
449450
let bytes = unsafe UnsafeMutableRawBufferPointer(
450-
start: (_count == 0) ? nil : _start(),
451+
start: (_count == 0) ? nil : _pointer,
451452
count: _count &* MemoryLayout<Element>.stride
452453
)
453454
return try unsafe body(bytes)
@@ -462,6 +463,7 @@ extension MutableSpan {
462463
@_alwaysEmitIntoClient
463464
@lifetime(self: copy self)
464465
public mutating func update(repeating repeatedValue: consuming Element) {
466+
guard !isEmpty else { return }
465467
unsafe _start().withMemoryRebound(to: Element.self, capacity: count) {
466468
unsafe $0.update(repeating: repeatedValue, count: count)
467469
}
@@ -599,7 +601,7 @@ extension MutableSpan where Element: BitwiseCopyable {
599601
) where Element: BitwiseCopyable {
600602
guard count > 0 else { return }
601603
// rebind _start manually in order to avoid assumptions about alignment.
602-
let rp = _start()._rawValue
604+
let rp = unsafe _start()._rawValue
603605
let binding = Builtin.bindMemory(rp, count._builtinWordValue, Element.self)
604606
let rebound = unsafe UnsafeMutablePointer<Element>(rp)
605607
unsafe rebound.update(repeating: repeatedValue, count: count)
@@ -796,7 +798,7 @@ extension MutableSpan where Element: ~Copyable {
796798
@_alwaysEmitIntoClient
797799
@lifetime(&self)
798800
mutating public func extracting(_: UnboundedRange) -> Self {
799-
let newSpan = unsafe Self(_unchecked: _start(), count: _count)
801+
let newSpan = unsafe Self(_unchecked: _pointer, count: _count)
800802
return unsafe _overrideLifetime(newSpan, mutating: &self)
801803
}
802804
}

0 commit comments

Comments
 (0)