Skip to content

[stdlib] tolerate empty source buffers in UMRBP.copyMemory #41836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ extension Unsafe${Mutable}RawBufferPointer {
public func copyMemory(from source: UnsafeRawBufferPointer) {
_debugPrecondition(source.count <= self.count,
"${Self}.copyMemory source has too many elements")
baseAddress?.copyMemory(from: source.baseAddress!, byteCount: source.count)
if let baseAddress = baseAddress, let sourceAddress = source.baseAddress {
baseAddress.copyMemory(from: sourceAddress, byteCount: source.count)
}
}

/// Copies from a collection of `UInt8` into this buffer's memory.
Expand Down
24 changes: 24 additions & 0 deletions validation-test/stdlib/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ ${SelfName}TestSuite.test("nilBaseAddress") {
expectEqualSequence([], emptyBuffer)
}

% if IsMutable:
${SelfName}TestSuite.test("copyFromEmptyBuffer") {
% if IsRaw:
var memory: UnsafeMutableRawPointer
let empty = UnsafeRawBufferPointer(start: nil, count: 0)
% else:
var memory: UnsafeMutablePointer<Float>
let empty = UnsafeBufferPointer<Float>(start: nil, count: 0)
% end

let count = 4
memory = allocateFor${Raw}Buffer(count: count)
defer { deallocateFor${Raw}Buffer(memory, count: count) }

let buffer = UnsafeMutable${Raw}BufferPointer(start: memory, count: count)

% if IsRaw:
buffer.copyMemory(from: empty)
% else:
_ = buffer.initialize(from: empty)
% end
}
% end

${SelfName}TestSuite.test("nonNilButEmpty") {
let emptyAllocated = UnsafeMutablePointer<Float>.allocate(capacity: 0)
defer { emptyAllocated.deallocate() }
Expand Down