Skip to content

Commit 8c68478

Browse files
authored
Merge pull request swiftlang#5007 from readdle/win-url-fsr-utf8
Allocate buffer of correct size in `NSURL.fileSystemRepresentation` on Windows
2 parents 543ae3c + 17051b4 commit 8c68478

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Sources/Foundation/NSURL.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,12 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
486486
#if os(Windows)
487487
if let resolved = CFURLCopyAbsoluteURL(_cfObject),
488488
let representation = CFURLCopyFileSystemPath(resolved, kCFURLWindowsPathStyle)?._swiftObject {
489-
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: representation.count + 1)
490-
representation.withCString { buffer.initialize(from: $0, count: representation.count + 1) }
491-
buffer[representation.count] = 0
489+
let buffer = representation.withCString {
490+
let len = strlen($0)
491+
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: len + 1)
492+
buffer.initialize(from: $0, count: len + 1)
493+
return buffer
494+
}
492495
return UnsafePointer(buffer)
493496
}
494497
#else

0 commit comments

Comments
 (0)