Skip to content

Allocate buffer of correct size in NSURL.fileSystemRepresentation on Windows #5007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Sources/Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int8>.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<Int8>.allocate(capacity: len + 1)
buffer.initialize(from: $0, count: len + 1)
return buffer
}
return UnsafePointer(buffer)
}
#else
Expand Down