Skip to content

[5.0] _BodyFileSource: Fix use after free in init() #2055

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
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
19 changes: 10 additions & 9 deletions Foundation/URLSession/BodySource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ extension _BodyDataSource : _BodySource {
/// have to be thread safe.
internal final class _BodyFileSource {
fileprivate let fileURL: URL
fileprivate let channel: DispatchIO
fileprivate let workQueue: DispatchQueue
fileprivate let channel: DispatchIO
fileprivate let workQueue: DispatchQueue
fileprivate let dataAvailableHandler: () -> Void
fileprivate var hasActiveReadHandler = false
fileprivate var availableChunk: _Chunk = .empty

/// Create a new data source backed by a file.
///
/// - Parameter fileURL: the file to read from
Expand All @@ -156,13 +157,13 @@ internal final class _BodyFileSource {
self.fileURL = fileURL
self.workQueue = workQueue
self.dataAvailableHandler = dataAvailableHandler
var fileSystemRepresentation: UnsafePointer<Int8>! = nil
fileURL.withUnsafeFileSystemRepresentation {
fileSystemRepresentation = $0
}
guard let channel = DispatchIO(type: .stream, path: fileSystemRepresentation,
oflag: O_RDONLY, mode: 0, queue: workQueue,
cleanupHandler: {_ in }) else {

guard let channel = fileURL.withUnsafeFileSystemRepresentation({
// DisptachIO (dispatch_io_create_with_path) makes a copy of the path
DispatchIO(type: .stream, path: $0!,
oflag: O_RDONLY, mode: 0, queue: workQueue,
cleanupHandler: {_ in })
}) else {
fatalError("Cant create DispatchIO channel")
}
self.channel = channel
Expand Down