Skip to content

Commit e40e0f7

Browse files
committed
[stdlib] improve UnsafeMutableRawBufferPointer.copyMemory
- The previous implementation trapped when the source buffer was empty. That behaviour is both not documented and unnecessary. If the source buffer is empty, its length is necessarily shorter or equal than the length of the destination. - The updated version simply returns when the source buffer is empty.
1 parent 5674086 commit e40e0f7

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ extension Unsafe${Mutable}RawBufferPointer {
438438
public func copyMemory(from source: UnsafeRawBufferPointer) {
439439
_debugPrecondition(source.count <= self.count,
440440
"${Self}.copyMemory source has too many elements")
441-
baseAddress?.copyMemory(from: source.baseAddress!, byteCount: source.count)
441+
if let baseAddress = baseAddress, let sourceAddress = source.baseAddress {
442+
baseAddress.copyMemory(from: sourceAddress, byteCount: source.count)
443+
}
442444
}
443445

444446
/// Copies from a collection of `UInt8` into this buffer's memory.

0 commit comments

Comments
 (0)