Skip to content

Commit 5ba9e8f

Browse files
authored
Merge pull request #79 from apple/glessard/binding-fixes
Rebind byte types safely
2 parents 82a6704 + 914c782 commit 5ba9e8f

File tree

4 files changed

+27
-41
lines changed

4 files changed

+27
-41
lines changed

Sources/System/FileOperations.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,12 @@ extension FileDescriptor {
391391
internal static func _pipe() -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> {
392392
var fds: (Int32, Int32) = (-1, -1)
393393
return withUnsafeMutablePointer(to: &fds) { pointer in
394-
valueOrErrno(retryOnInterrupt: false) {
395-
system_pipe(UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: Int32.self))
394+
pointer.withMemoryRebound(to: Int32.self, capacity: 2) { fds in
395+
valueOrErrno(retryOnInterrupt: false) {
396+
system_pipe(fds)
397+
}.map { _ in (.init(rawValue: fds[0]), .init(rawValue: fds[1])) }
396398
}
397-
}.map { _ in (.init(rawValue: fds.0), .init(rawValue: fds.1)) }
399+
}
398400
}
399401
}
400402
#endif

Sources/System/SystemString.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ extension SystemString {
171171
internal func withCodeUnits<T>(
172172
_ f: (UnsafeBufferPointer<CInterop.PlatformUnicodeEncoding.CodeUnit>) throws -> T
173173
) rethrows -> T {
174-
try withSystemChars {
175-
// TODO: Is this the right way?
176-
let base = UnsafeRawPointer(
177-
$0.baseAddress
178-
)!.assumingMemoryBound(to: CInterop.PlatformUnicodeEncoding.CodeUnit.self)
179-
return try f(UnsafeBufferPointer(start: base, count: $0.count))
174+
try withSystemChars { chars in
175+
let length = chars.count * MemoryLayout<SystemChar>.stride
176+
let count = length / MemoryLayout<CInterop.PlatformUnicodeEncoding.CodeUnit>.stride
177+
return try chars.baseAddress!.withMemoryRebound(
178+
to: CInterop.PlatformUnicodeEncoding.CodeUnit.self,
179+
capacity: count
180+
) { pointer in
181+
try f(UnsafeBufferPointer(start: pointer, count: count))
182+
}
180183
}
181184
}
182185
}
@@ -272,13 +275,15 @@ extension SystemString {
272275
internal func withPlatformString<T>(
273276
_ f: (UnsafePointer<CInterop.PlatformChar>) throws -> T
274277
) rethrows -> T {
275-
try withSystemChars {
276-
// TODO: Is this the right way?
277-
let base = UnsafeRawPointer(
278-
$0.baseAddress
279-
)!.assumingMemoryBound(to: CInterop.PlatformChar.self)
280-
assert(base[self.count] == 0)
281-
return try f(base)
278+
try withSystemChars { chars in
279+
let length = chars.count * MemoryLayout<SystemChar>.stride
280+
return try chars.baseAddress!.withMemoryRebound(
281+
to: CInterop.PlatformChar.self,
282+
capacity: length / MemoryLayout<CInterop.PlatformChar>.stride
283+
) { pointer in
284+
assert(pointer[self.count] == 0)
285+
return try f(pointer)
286+
}
282287
}
283288
}
284289
}

Tests/SystemTests/FilePathTests/FilePathTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ func filePathFromUnterminatedBytes<S: Sequence>(_ bytes: S) -> FilePath where S.
2222
array += [0]
2323

2424
return array.withUnsafeBufferPointer {
25-
FilePath(platformString: $0.baseAddress!._asCChar)
25+
$0.withMemoryRebound(to: CChar.self) {
26+
FilePath(platformString: $0.baseAddress!)
27+
}
2628
}
2729
}
2830
let invalidBytes: [UInt8] = [0x2F, 0x61, 0x2F, 0x62, 0x2F, 0x83]

Tests/SystemTests/SystemStringTests.swift

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,6 @@
1010
// Tests for PlatformString, SystemString, and FilePath's forwarding APIs
1111

1212
// TODO: Adapt test to Windows
13-
extension UnsafePointer where Pointee == UInt8 {
14-
internal var _asCChar: UnsafePointer<CChar> {
15-
UnsafeRawPointer(self).assumingMemoryBound(to: CChar.self)
16-
}
17-
}
18-
extension UnsafePointer where Pointee == CChar {
19-
internal var _asUInt8: UnsafePointer<UInt8> {
20-
UnsafeRawPointer(self).assumingMemoryBound(to: UInt8.self)
21-
}
22-
}
23-
extension UnsafeBufferPointer where Element == UInt8 {
24-
internal var _asCChar: UnsafeBufferPointer<CChar> {
25-
let base = baseAddress?._asCChar
26-
return UnsafeBufferPointer<CChar>(start: base, count: self.count)
27-
}
28-
}
29-
extension UnsafeBufferPointer where Element == CChar {
30-
internal var _asUInt8: UnsafeBufferPointer<UInt8> {
31-
let base = baseAddress?._asUInt8
32-
return UnsafeBufferPointer<UInt8>(start: base, count: self.count)
33-
}
34-
}
35-
3613

3714
import XCTest
3815

@@ -46,7 +23,7 @@ private func makeRaw(
4623
_ str: String
4724
) -> [CInterop.PlatformChar] {
4825
var str = str
49-
var array = str.withUTF8 { Array($0._asCChar) }
26+
var array = str.withUTF8 { $0.withMemoryRebound(to: CChar.self, Array.init) }
5027
array.append(0)
5128
return array
5229
}

0 commit comments

Comments
 (0)