Skip to content

Commit 5b56dc8

Browse files
authored
Merge pull request #2809 from valeriyvan/NSURL
Eliminates redundant buffer zeroing in NSURL
2 parents aa07812 + 42c9398 commit 5b56dc8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Sources/Foundation/NSURL.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
527527

528528
// For historical reasons, the password string should _not_ have its percent escapes removed.
529529
let bufSize = CFURLGetBytes(absoluteURL, nil, 0)
530-
var buf = [UInt8](repeating: 0, count: bufSize)
531-
guard CFURLGetBytes(absoluteURL, &buf, bufSize) >= 0 else {
532-
return nil
530+
let buf = [UInt8](unsafeUninitializedCapacity: bufSize) { buffer, initializedCount in
531+
initializedCount = CFURLGetBytes(absoluteURL, buffer.baseAddress, buffer.count)
532+
precondition(initializedCount == bufSize, "Inconsistency in CFURLGetBytes")
533533
}
534-
534+
535535
let passwordBuf = buf[passwordRange.location ..< passwordRange.location+passwordRange.length]
536536
return passwordBuf.withUnsafeBufferPointer { ptr in
537537
NSString(bytes: ptr.baseAddress!, length: passwordBuf.count, encoding: String.Encoding.utf8.rawValue)?._swiftObject

0 commit comments

Comments
 (0)