Skip to content

Commit bc7b7ff

Browse files
committed
_BodyFileSource: Fix use after free in init()
- init(fileURL:workQueue:dataAvailableHandler:) was using the closure argument for the buffer pointer outside of the closure, after .deallocate() was called. (cherry picked from commit 21549ff)
1 parent a9649e5 commit bc7b7ff

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Foundation/URLSession/BodySource.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,12 @@ extension _BodyDataSource : _BodySource {
135135
/// have to be thread safe.
136136
internal final class _BodyFileSource {
137137
fileprivate let fileURL: URL
138-
fileprivate let channel: DispatchIO
139-
fileprivate let workQueue: DispatchQueue
138+
fileprivate let channel: DispatchIO
139+
fileprivate let workQueue: DispatchQueue
140140
fileprivate let dataAvailableHandler: () -> Void
141141
fileprivate var hasActiveReadHandler = false
142142
fileprivate var availableChunk: _Chunk = .empty
143+
143144
/// Create a new data source backed by a file.
144145
///
145146
/// - Parameter fileURL: the file to read from
@@ -156,13 +157,13 @@ internal final class _BodyFileSource {
156157
self.fileURL = fileURL
157158
self.workQueue = workQueue
158159
self.dataAvailableHandler = dataAvailableHandler
159-
var fileSystemRepresentation: UnsafePointer<Int8>! = nil
160-
fileURL.withUnsafeFileSystemRepresentation {
161-
fileSystemRepresentation = $0
162-
}
163-
guard let channel = DispatchIO(type: .stream, path: fileSystemRepresentation,
164-
oflag: O_RDONLY, mode: 0, queue: workQueue,
165-
cleanupHandler: {_ in }) else {
160+
161+
guard let channel = fileURL.withUnsafeFileSystemRepresentation({
162+
// DisptachIO (dispatch_io_create_with_path) makes a copy of the path
163+
DispatchIO(type: .stream, path: $0!,
164+
oflag: O_RDONLY, mode: 0, queue: workQueue,
165+
cleanupHandler: {_ in })
166+
}) else {
166167
fatalError("Cant create DispatchIO channel")
167168
}
168169
self.channel = channel

0 commit comments

Comments
 (0)