Skip to content

[4.2] FileSystemRepresentation: Avoid leak and over early free. #2071

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,14 @@ open class FileManager : NSObject {
let _fsRep: UnsafePointer<Int8>
if fsRep == nil {
_fsRep = fileSystemRepresentation(withPath: path)
defer { _fsRep.deallocate() }
} else {
_fsRep = fsRep!
}

defer {
if fsRep == nil { _fsRep.deallocate() }
}

var statInfo = stat()
guard lstat(_fsRep, &statInfo) == 0 else {
throw _NSErrorWithErrno(errno, reading: true, path: path)
Expand Down
4 changes: 3 additions & 1 deletion Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,9 @@ public struct URL : ReferenceConvertible, Equatable {
/// File system representation is a null-terminated C string with canonical UTF-8 encoding.
/// - note: The pointer is not valid outside the context of the block.
public func withUnsafeFileSystemRepresentation<ResultType>(_ block: (UnsafePointer<Int8>?) throws -> ResultType) rethrows -> ResultType {
return try block(_url.fileSystemRepresentation)
let fsRep = _url.fileSystemRepresentation
defer { fsRep.deallocate() }
return try block(fsRep)
}

// MARK: -
Expand Down