From b56140a7c0b9f35acabcfbe3592c4485c3270176 Mon Sep 17 00:00:00 2001 From: tid Date: Mon, 2 Apr 2018 02:20:59 +0900 Subject: [PATCH] [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. --- Foundation/NSString.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Foundation/NSString.swift b/Foundation/NSString.swift index 7a8dc235de..20ea403067 100644 --- a/Foundation/NSString.swift +++ b/Foundation/NSString.swift @@ -294,6 +294,10 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC } internal func _fastCStringContents(_ nullTerminated: Bool) -> UnsafePointer? { + guard !nullTerminated else { + // There is no way to fastly and safely retrieve a pointer to a null-terminated string from a String of Swift. + return nil + } if type(of: self) == NSString.self || type(of: self) == NSMutableString.self { if _storage._guts._isContiguousASCII { return unsafeBitCast(_storage._core.startASCII, to: UnsafePointer.self)