From b1a9c732066ab61ff241ef0033deee9b08292e48 Mon Sep 17 00:00:00 2001 From: Mark Lacey Date: Tue, 15 May 2018 20:21:44 -0700 Subject: [PATCH] Use withUnsafeMutableBufferPointer in NSData test. This ensures that: - We produce a well scoped stable pointer for the duration of the use. - We aren't attempting to do an implicit conversion from an array to pointer in an argument position which is an autoclosure result, which is now disallowed in https://github.com/apple/swift/pull/16623. --- TestFoundation/TestNSData.swift | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/TestFoundation/TestNSData.swift b/TestFoundation/TestNSData.swift index 10b74c43a4..015f4b577e 100644 --- a/TestFoundation/TestNSData.swift +++ b/TestFoundation/TestNSData.swift @@ -1,6 +1,6 @@ // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information @@ -600,10 +600,15 @@ class TestNSData: LoopbackServerTest { func test_edgeNoCopyDescription() { let expected = "" - let bytes = [UInt8](repeating: 0xff, count: 1025) - let data = NSData(bytesNoCopy: UnsafeMutablePointer(mutating: bytes), length: bytes.count, freeWhenDone: false) - XCTAssertEqual(data.debugDescription, expected) - XCTAssertEqual(data.bytes, bytes) + var bytes = [UInt8](repeating: 0xff, count: 1025) + + bytes.withUnsafeMutableBufferPointer { + let baseAddress = $0.baseAddress! + let count = $0.count + let data = NSData(bytesNoCopy: UnsafeMutableRawPointer(baseAddress), length: count, freeWhenDone: false) + XCTAssertEqual(data.debugDescription, expected) + XCTAssertEqual(data.bytes, UnsafeRawPointer(baseAddress)) + } } func test_initializeWithBase64EncodedDataGetsDecodedData() {