Skip to content

Commit b883eca

Browse files
authored
Merge pull request #2676 from compnerd/corruption
Revert "Foundation: fix missing string terminator"
2 parents 3f1c097 + 8180c48 commit b883eca

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Docs/ReleaseNotes_Swift5.md

+8
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ A few notes:
156156
* The exception-throwing API will be deprecated in a future release. It is now marked as `@available(…, deprecated: 100000, …)`, which matches what you would see if `API_TO_BE_DEPRECATED` was used in a Objective-C header.
157157

158158
* Subclassing `NSFileHandle` is strongly discouraged. Many of the new methods are `public` and cannot be overridden.
159+
160+
## `NSString(bytesNoCopy:length:encoding:freeWhenDone:)` deviations from reference implementation
161+
162+
On Windows, `NSString(bytesNoCopy:length:encoding:freeWhenDone:)` deviates from
163+
the behaviour on Darwin and uses the buffer's `deallocate` routine rather than
164+
`free`. This is done to ensure that the correct routine is invoked for
165+
releasing the resources acquired through `UnsafeMutablePointer.allocate` or
166+
`UnsafeMutableRawPointer.allocate`.

Sources/Foundation/NSData.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,10 @@ 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() }
603602
let buffer = UnsafeMutableRawBufferPointer(start: ptr, count: capacity)
604603
let length = NSData.base64EncodeBytes(self, options: options, buffer: buffer)
605604

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

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

Sources/Foundation/NSString.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,11 @@ extension NSString {
13591359
// just copy for now since the internal storage will be a copy anyhow
13601360
self.init(bytes: bytes, length: len, encoding: encoding)
13611361
if freeBuffer { // don't take the hint
1362-
free(bytes)
1362+
#if os(Windows)
1363+
bytes.deallocate()
1364+
#else
1365+
free(bytes)
1366+
#endif
13631367
}
13641368
}
13651369

0 commit comments

Comments
 (0)