Skip to content

Commit 1ae74c0

Browse files
committed
SR-10240: Dont try to write an empty Data() if it has a nil baseAddress.
1 parent c9d823c commit 1ae74c0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Foundation/FileHandle.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ open class FileHandle : NSObject, NSSecureCoding {
507507

508508
for region in data.regions {
509509
try region.withUnsafeBytes { (bytes) in
510-
try _writeBytes(buf: UnsafeRawPointer(bytes.baseAddress!), length: bytes.count)
510+
if let baseAddress = bytes.baseAddress, bytes.count > 0 {
511+
try _writeBytes(buf: UnsafeRawPointer(baseAddress), length: bytes.count)
512+
}
511513
}
512514
}
513515
}

TestFoundation/TestPipe.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class TestPipe: XCTestCase {
4343
// First write some data into the pipe
4444
let stringAsData = try text.data(using: .utf8).unwrapped()
4545
try aPipe.fileHandleForWriting.write(contentsOf: stringAsData)
46-
46+
47+
// SR-10240 - Check empty Data() can be written without crashing
48+
aPipe.fileHandleForWriting.write(Data())
49+
4750
// Then read it out again
4851
let data = try aPipe.fileHandleForReading.read(upToCount: stringAsData.count).unwrapped()
4952

0 commit comments

Comments
 (0)