Skip to content

Commit 7b1d3fe

Browse files
authored
Merge pull request swiftlang#2662 from compnerd/invalid-free
Foundation: fix invalid free
2 parents cc9e394 + f3a6f50 commit 7b1d3fe

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Foundation/NSData.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,12 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
599599

600600
let capacity = estimateBase64Size(length: dataLength)
601601
let ptr = UnsafeMutableRawPointer.allocate(byteCount: capacity, alignment: 1)
602+
defer { ptr.deallocate() }
602603
let buffer = UnsafeMutableRawBufferPointer(start: ptr, count: capacity)
603604
let length = NSData.base64EncodeBytes(self, options: options, buffer: buffer)
604605

605-
return String(bytesNoCopy: ptr, length: length, encoding: .ascii, freeWhenDone: true)!
606+
let utf8buffer = UnsafeBufferPointer<UInt8>(start: ptr.assumingMemoryBound(to: UInt8.self), count: length)
607+
return String(decoding: utf8buffer, as: UTF8.self)
606608
}
607609

608610
/// Creates a Base64, UTF-8 encoded Data from the data object using the given options.

0 commit comments

Comments
 (0)