Skip to content

Commit b56140a

Browse files
committed
[SR-7112] Fixed a case where CFStringGetCStringPtr returns a string without null termination
Changed _fastCStringContents (used in CFStringGetCStringPtr) to return NULL if required to return a string containing null termination. Since this fix, if you call CFStringGetCStringPtr with a Swift-based CFString, it returns NULL. Before the change: _fastCStringContents always returned a pointer to an ASCII string containing no NULL termination. This is because the code was using unsafeBitCast to get internal pointer of String of Swift. Swift's string does not guarantee null termination.
1 parent 4f2ca47 commit b56140a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

Foundation/NSString.swift

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
294294
}
295295

296296
internal func _fastCStringContents(_ nullTerminated: Bool) -> UnsafePointer<Int8>? {
297+
guard !nullTerminated else {
298+
// There is no way to fastly and safely retrieve a pointer to a null-terminated string from a String of Swift.
299+
return nil
300+
}
297301
if type(of: self) == NSString.self || type(of: self) == NSMutableString.self {
298302
if _storage._guts._isContiguousASCII {
299303
return unsafeBitCast(_storage._core.startASCII, to: UnsafePointer<Int8>.self)

0 commit comments

Comments
 (0)