From 17051b41214da40ca715b1a6bc8eda6d627a629b Mon Sep 17 00:00:00 2001 From: Alexander Smarus Date: Thu, 11 Jul 2024 16:02:42 +0200 Subject: [PATCH] Allocate buffer of correct size in `NSURL.fileSystemRepresentation` on Windows --- Sources/Foundation/NSURL.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/Foundation/NSURL.swift b/Sources/Foundation/NSURL.swift index 4d31e2ba9c..7a3f09b4bc 100644 --- a/Sources/Foundation/NSURL.swift +++ b/Sources/Foundation/NSURL.swift @@ -486,9 +486,12 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying { #if os(Windows) if let resolved = CFURLCopyAbsoluteURL(_cfObject), let representation = CFURLCopyFileSystemPath(resolved, kCFURLWindowsPathStyle)?._swiftObject { - let buffer = UnsafeMutablePointer.allocate(capacity: representation.count + 1) - representation.withCString { buffer.initialize(from: $0, count: representation.count + 1) } - buffer[representation.count] = 0 + let buffer = representation.withCString { + let len = strlen($0) + let buffer = UnsafeMutablePointer.allocate(capacity: len + 1) + buffer.initialize(from: $0, count: len + 1) + return buffer + } return UnsafePointer(buffer) } #else