Skip to content

Commit 4a3fe0d

Browse files
authored
Merge pull request #2640 from spevans/pr_dangling_pointer
2 parents b14c0df + 4cf2543 commit 4a3fe0d

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

Foundation/NSData.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,22 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
207207
/// Initializes a data object with the given Base64 encoded string.
208208
public init?(base64Encoded base64String: String, options: Base64DecodingOptions = []) {
209209
let encodedBytes = Array(base64String.utf8)
210-
guard let decodedBytes = NSData.base64DecodeBytes(encodedBytes, options: options) else {
210+
guard var decodedBytes = NSData.base64DecodeBytes(encodedBytes, options: options) else {
211211
return nil
212212
}
213213
super.init()
214-
_init(bytes: UnsafeMutableRawPointer(mutating: decodedBytes), length: decodedBytes.count, copy: true)
214+
_init(bytes: &decodedBytes, length: decodedBytes.count, copy: true)
215215
}
216216

217217
/// Initializes a data object with the given Base64 encoded data.
218218
public init?(base64Encoded base64Data: Data, options: Base64DecodingOptions = []) {
219219
var encodedBytes = [UInt8](repeating: 0, count: base64Data.count)
220220
base64Data._nsObject.getBytes(&encodedBytes, length: encodedBytes.count)
221-
guard let decodedBytes = NSData.base64DecodeBytes(encodedBytes, options: options) else {
221+
guard var decodedBytes = NSData.base64DecodeBytes(encodedBytes, options: options) else {
222222
return nil
223223
}
224224
super.init()
225-
_init(bytes: UnsafeMutableRawPointer(mutating: decodedBytes), length: decodedBytes.count, copy: true)
225+
_init(bytes: &decodedBytes, length: decodedBytes.count, copy: true)
226226
}
227227

228228
deinit {

TestFoundation/TestJSONSerialization.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,11 +1415,11 @@ extension TestJSONSerialization {
14151415
}
14161416
}
14171417

1418-
func test_jsonObjectToOutputStreamBuffer(){
1418+
func test_jsonObjectToOutputStreamBuffer() {
14191419
let dict = ["a":["b":1]]
14201420
do {
1421-
let buffer = Array<UInt8>(repeating: 0, count: 20)
1422-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 20)
1421+
var buffer = Array<UInt8>(repeating: 0, count: 20)
1422+
let outputStream = OutputStream(toBuffer: &buffer, capacity: buffer.count)
14231423
outputStream.open()
14241424
let result = try JSONSerialization.writeJSONObject(dict, toStream: outputStream, options: [])
14251425
outputStream.close()
@@ -1463,8 +1463,8 @@ extension TestJSONSerialization {
14631463
func test_jsonObjectToOutputStreamInsufficientBuffer() {
14641464
#if !DARWIN_COMPATIBILITY_TESTS // Hangs
14651465
let dict = ["a":["b":1]]
1466-
let buffer = Array<UInt8>(repeating: 0, count: 10)
1467-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
1466+
var buffer = Array<UInt8>(repeating: 0, count: 10)
1467+
let outputStream = OutputStream(toBuffer: &buffer, capacity: buffer.count)
14681468
outputStream.open()
14691469
do {
14701470
let result = try JSONSerialization.writeJSONObject(dict, toStream: outputStream, options: [])

TestFoundation/TestNSKeyedArchiver.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,16 @@ class TestNSKeyedArchiver : XCTestCase {
277277

278278
func test_archive_charptr() {
279279
let charArray = [CChar]("Hello world, we are testing!\0".utf8CString)
280-
var charPtr = UnsafeMutablePointer(mutating: charArray)
281280

282-
test_archive({ archiver -> Bool in
281+
charArray.withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<CChar>) in
282+
var charPtr = buffer.baseAddress!
283+
test_archive({ archiver -> Bool in
283284
let value = NSValue(bytes: &charPtr, objCType: "*")
284285

285286
archiver.encode(value, forKey: "root")
286287
return true
287288
},
288-
decode: {unarchiver -> Bool in
289+
decode: {unarchiver -> Bool in
289290
guard let value = unarchiver.decodeObject(of: NSValue.self, forKey: "root") else {
290291
return false
291292
}
@@ -299,7 +300,8 @@ class TestNSKeyedArchiver : XCTestCase {
299300

300301
return s1 == s2
301302
}
302-
})
303+
})
304+
}
303305
}
304306

305307
func test_archive_user_class() {

TestFoundation/TestNSValue.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ class TestNSValue : XCTestCase {
110110
}
111111

112112
func test_valueWithCharPtr() {
113-
let charArray = [UInt8]("testing123".utf8)
114-
var charPtr = UnsafeMutablePointer(mutating: charArray)
115-
var expectedPtr: UnsafeMutablePointer<UInt8>? = nil
113+
var charArray = [UInt8]("testing123".utf8)
114+
charArray.withUnsafeMutableBufferPointer {
115+
var charPtr = $0.baseAddress!
116+
var expectedPtr: UnsafeMutablePointer<UInt8>? = nil
116117

117-
NSValue(bytes: &charPtr, objCType: "*").getValue(&expectedPtr)
118-
XCTAssertEqual(charPtr, expectedPtr)
118+
NSValue(bytes: &charPtr, objCType: "*").getValue(&expectedPtr)
119+
XCTAssertEqual(charPtr, expectedPtr)
120+
}
119121
}
120122

121123
func test_isEqual() {

TestFoundation/TestStream.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class TestStream : XCTestCase {
195195
var buffer = Array<UInt8>(repeating: 0, count: 12)
196196
let myString = "Hello world!"
197197
let encodedData = [UInt8](myString.utf8)
198-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 12)
198+
let outputStream = OutputStream(toBuffer: &buffer, capacity: buffer.count)
199199
XCTAssertEqual(.notOpen, outputStream.streamStatus)
200200
outputStream.open()
201201
XCTAssertEqual(.open, outputStream.streamStatus)
@@ -238,19 +238,19 @@ class TestStream : XCTestCase {
238238
//verify the data written
239239
let dataWritten = outputStream.property(forKey: Stream.PropertyKey.dataWrittenToMemoryStreamKey)
240240
if let nsdataWritten = dataWritten as? NSData {
241-
nsdataWritten.getBytes(UnsafeMutablePointer(mutating: buffer), length: result!)
242-
XCTAssertEqual(NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), NSString(string: myString))
241+
nsdataWritten.getBytes(&buffer, length: result!)
242+
XCTAssertEqual(NSString(bytes: buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), NSString(string: myString))
243243
outputStream.close()
244244
} else {
245-
XCTFail("Unable to get data from memeory.")
245+
XCTFail("Unable to get data from memory.")
246246
}
247247
}
248248

249249
func test_outputStreamHasSpaceAvailable() {
250-
let buffer = Array<UInt8>(repeating: 0, count: 12)
250+
var buffer = Array<UInt8>(repeating: 0, count: 12)
251251
let myString = "Welcome To Hello world !"
252252
let encodedData = [UInt8](myString.utf8)
253-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 12)
253+
let outputStream = OutputStream(toBuffer: &buffer, capacity: buffer.count)
254254
outputStream.open()
255255
XCTAssertTrue(outputStream.hasSpaceAvailable)
256256
_ = outputStream.write(encodedData, maxLength: encodedData.count)

0 commit comments

Comments
 (0)