Skip to content

Commit 3b9a917

Browse files
committed
Revert "Revert "UnsafePointer conversion fixes.""
This reverts commit 965c6af. This reapplies pointer conversion fixed from PR #487. The previous failure turned out to be a spurious device error.
1 parent 965c6af commit 3b9a917

16 files changed

+96
-80
lines changed

Foundation/Data.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
130130

131131
This type provides "copy-on-write" behavior, and is also bridged to the Objective-C `NSData` class. You can wrap an instance of a custom subclass of `NSData` in `struct Data` by converting it using `myData as Data`.
132132

133-
`Data` can be initialized with an `UnsafePointer` and count, an array of `UInt8` (the primitive byte type), or an `UnsafeBufferPointer`. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
133+
`Data` can be initialized with an `UnsafeRawPointer` and count, an array of `UInt8` (the primitive byte type), an `UnsafeBufferPointer`,the contents of a file, or base-64 encoded data or strings. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
134134
*/
135135
public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, Hashable, RandomAccessCollection, MutableCollection, _MutablePairBoxing {
136136
/// The Objective-C bridged type of `Data`.
@@ -161,7 +161,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
161161
case none
162162

163163
/// A custom deallocator.
164-
case custom((UnsafeMutablePointer<UInt8>, Int) -> Void)
164+
case custom((UnsafeMutableRawPointer, Int) -> Void)
165165

166166
fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
167167
switch self {
@@ -173,8 +173,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
173173
return nil
174174
case .custom(let b):
175175
return { (ptr, len) in
176-
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: len)
177-
b(bytePtr, len)
176+
b(ptr, len)
178177
}
179178
}
180179
}
@@ -238,7 +237,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
238237
/// - parameter bytes: A pointer to the bytes.
239238
/// - parameter count: The size of the bytes.
240239
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer, or `.none`.
241-
public init(bytesNoCopy bytes: UnsafeMutablePointer<UInt8>, count: Int, deallocator: Deallocator) {
240+
public init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Deallocator) {
242241
let whichDeallocator = deallocator._deallocator
243242
_wrapped = _SwiftNSData(immutableObject: NSData(bytesNoCopy: bytes, length: count, deallocator: whichDeallocator))
244243
}
@@ -348,7 +347,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
348347
_mapUnmanaged { $0.getBytes(pointer, length: count) }
349348
}
350349

351-
private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: NSRange) {
350+
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: NSRange) {
352351
_mapUnmanaged { $0.getBytes(pointer, range: range) }
353352
}
354353

@@ -389,7 +388,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
389388
guard !copyRange.isEmpty else { return 0 }
390389

391390
let nsRange = NSMakeRange(copyRange.lowerBound, copyRange.upperBound - copyRange.lowerBound)
392-
let pointer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(buffer.baseAddress!)
391+
let pointer = UnsafeMutableRawPointer(buffer.baseAddress!)
393392
_copyBytesHelper(to: pointer, from: nsRange)
394393
return copyRange.count
395394
}

Foundation/NSFileManager.swift

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,12 @@ public class FileManager: NSObject {
252252
}
253253

254254
while let entry = readdir(dir!) {
255-
if let entryName = withUnsafePointer(to: &entry.pointee.d_name, { (ptr) -> String? in
256-
return String(cString: UnsafePointer<Int8>(ptr))
257-
}) {
258-
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
259-
if entryName != "." && entryName != ".." {
260-
contents.append(entryName)
261-
}
255+
let entryName = withUnsafePointer(to: &entry.pointee.d_name) {
256+
String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
257+
}
258+
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
259+
if entryName != "." && entryName != ".." {
260+
contents.append(entryName)
262261
}
263262
}
264263

@@ -294,31 +293,27 @@ public class FileManager: NSObject {
294293
var entry = readdir(dir!)
295294

296295
while entry != nil {
297-
if let entryName = withUnsafePointer(to: &entry!.pointee.d_name, { (ptr) -> String? in
298-
let int8Ptr = unsafeBitCast(ptr, to: UnsafePointer<Int8>.self)
299-
return String(cString: int8Ptr)
300-
}) {
301-
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
302-
if entryName != "." && entryName != ".." {
303-
contents.append(entryName)
296+
let entryName = withUnsafePointer(to: &entry!.pointee.d_name) {
297+
String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
298+
}
299+
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
300+
if entryName != "." && entryName != ".." {
301+
contents.append(entryName)
304302

305-
if let entryType = withUnsafePointer(to: &entry!.pointee.d_type, { (ptr) -> Int32? in
306-
let int32Ptr = unsafeBitCast(ptr, to: UnsafePointer<UInt8>.self)
307-
return Int32(int32Ptr.pointee)
308-
}) {
309-
#if os(OSX) || os(iOS)
310-
let tempEntryType = entryType
311-
#elseif os(Linux)
312-
let tempEntryType = Int(entryType)
313-
#endif
303+
let entryType = withUnsafePointer(to: &entry!.pointee.d_type) { (ptr) -> Int32 in
304+
return Int32(ptr.pointee)
305+
}
306+
#if os(OSX) || os(iOS)
307+
let tempEntryType = entryType
308+
#elseif os(Linux)
309+
let tempEntryType = Int(entryType)
310+
#endif
314311

315-
if tempEntryType == DT_DIR {
316-
let subPath: String = path + "/" + entryName
312+
if tempEntryType == DT_DIR {
313+
let subPath: String = path + "/" + entryName
317314

318-
let entries = try subpathsOfDirectory(atPath: subPath)
319-
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
320-
}
321-
}
315+
let entries = try subpathsOfDirectory(atPath: subPath)
316+
contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
322317
}
323318
}
324319

@@ -468,7 +463,7 @@ public class FileManager: NSObject {
468463

469464
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
470465
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
471-
ps.initialize(to: UnsafeMutablePointer(fsRep))
466+
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
472467
ps.advanced(by: 1).initialize(to: nil)
473468
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
474469
ps.deinitialize(count: 2)
@@ -914,7 +909,7 @@ extension FileManager {
914909
if FileManager.default().fileExists(atPath: path) {
915910
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
916911
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
917-
ps.initialize(to: UnsafeMutablePointer(fsRep))
912+
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
918913
ps.advanced(by: 1).initialize(to: nil)
919914
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
920915
ps.deinitialize(count: 2)

Foundation/NSJSONSerialization.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public class JSONSerialization : NSObject {
111111
pretty: opt.contains(.prettyPrinted),
112112
writer: { (str: String?) in
113113
if let str = str {
114-
result.append(UnsafePointer<UInt8>(str.cString(using: .utf8)!), count: str.lengthOfBytes(using: .utf8))
114+
let count = str.lengthOfBytes(using: .utf8)
115+
result.append(UnsafeRawPointer(str.cString(using: .utf8)!).bindMemory(to: UInt8.self, capacity: count), count: count)
115116
}
116117
}
117118
)
@@ -635,11 +636,11 @@ private struct JSONReader {
635636
func parseNumber(_ input: Index) throws -> (Any, Index)? {
636637
func parseTypedNumber(_ address: UnsafePointer<UInt8>, count: Int) -> (Any, IndexDistance)? {
637638
let temp_buffer_size = 64
638-
var temp_buffer = [UInt8](repeating: 0, count: temp_buffer_size)
639-
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<UInt8>) -> (Any, IndexDistance)? in
639+
var temp_buffer = [Int8](repeating: 0, count: temp_buffer_size)
640+
return temp_buffer.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<Int8>) -> (Any, IndexDistance)? in
640641
memcpy(buffer.baseAddress!, address, min(count, temp_buffer_size - 1)) // ensure null termination
641642

642-
let startPointer = UnsafePointer<Int8>(buffer.baseAddress!)
643+
let startPointer = buffer.baseAddress!
643644
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
644645
defer { intEndPointer.deallocate(capacity: 1) }
645646
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)

Foundation/NSRegularExpression.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ran
130130
#endif
131131
matcher.block(nil, NSMatchingFlags(rawValue: opts), UnsafeMutablePointer<ObjCBool>(stop))
132132
} else {
133-
let result = TextCheckingResult.regularExpressionCheckingResultWithRanges(NSRangePointer(ranges!), count: count, regularExpression: matcher.regex)
133+
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
134+
TextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
135+
}
134136
#if os(OSX) || os(iOS)
135137
let flags = NSMatchingFlags(rawValue: options.rawValue)
136138
#else

Foundation/NSString.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ extension NSString {
469469
var numCharsBuffered = 0
470470
var arrayBuffer = [unichar](repeating: 0, count: 100)
471471
let other = str._nsObject
472-
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(_fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
472+
return arrayBuffer.withUnsafeMutablePointerOrAllocation(selfLen, fastpath: UnsafeMutablePointer<unichar>(mutating: _fastContents)) { (selfChars: UnsafeMutablePointer<unichar>) -> String in
473473
// Now do the binary search. Note that the probe value determines the length of the substring to check.
474474
while true {
475475
let range = NSMakeRange(0, isLiteral ? probe + 1 : NSMaxRange(rangeOfComposedCharacterSequence(at: probe))) // Extend the end of the composed char sequence
@@ -1182,8 +1182,12 @@ extension NSString {
11821182
}
11831183

11841184
public convenience init?(UTF8String nullTerminatedCString: UnsafePointer<Int8>) {
1185-
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(nullTerminatedCString), count: Int(strlen(nullTerminatedCString)))
1186-
if let str = String._fromCodeUnitSequence(UTF8.self, input: buffer) {
1185+
let count = Int(strlen(nullTerminatedCString))
1186+
if let str = nullTerminatedCString.withMemoryRebound(to: UInt8.self, capacity: count, {
1187+
let buffer = UnsafeBufferPointer<UInt8>(start: $0, count: count)
1188+
return String._fromCodeUnitSequence(UTF8.self, input: buffer)
1189+
}) as String?
1190+
{
11871191
self.init(str)
11881192
} else {
11891193
return nil

Foundation/NSURL.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ public class NSURL: NSObject, NSSecureCoding, NSCopying {
488488
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding.
489489
*/
490490
public func getFileSystemRepresentation(_ buffer: UnsafeMutablePointer<Int8>, maxLength maxBufferLength: Int) -> Bool {
491-
return CFURLGetFileSystemRepresentation(_cfObject, true, UnsafeMutablePointer<UInt8>(buffer), maxBufferLength)
491+
return buffer.withMemoryRebound(to: UInt8.self, capacity: maxBufferLength) {
492+
CFURLGetFileSystemRepresentation(_cfObject, true, $0, maxBufferLength)
493+
}
492494
}
493495

494496
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding. The returned C string will be automatically freed just as a returned object would be released; your code should copy the representation or use getFileSystemRepresentation:maxLength: if it needs to store the representation outside of the autorelease context in which the representation is created.

Foundation/NSXMLElement.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ public class XMLElement: XMLNode {
7474
@abstract Adds an attribute. Attributes with duplicate names are not added.
7575
*/
7676
public func addAttribute(_ attribute: XMLNode) {
77-
guard _CFXMLNodeHasProp(_xmlNode, UnsafePointer<UInt8>(_CFXMLNodeGetName(attribute._xmlNode)!)) == nil else { return }
78-
addChild(attribute)
77+
let name = _CFXMLNodeGetName(attribute._xmlNode)!
78+
let len = strlen(name)
79+
name.withMemoryRebound(to: UInt8.self, capacity: Int(len)) {
80+
guard _CFXMLNodeHasProp(_xmlNode, $0) == nil else { return }
81+
addChild(attribute)
82+
}
7983
} //primitive
8084

8185
/*!

0 commit comments

Comments
 (0)