-
Notifications
You must be signed in to change notification settings - Fork 0
Mission-critical Swift audit: concurrency + file-read safety fixes #16
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,23 +25,25 @@ public struct TraceFileReader { | |
| } | ||
|
|
||
| do { | ||
| let attributes = try fileManager.attributesOfItem(atPath: url.path) | ||
| if let sizeValue = attributes[.size] as? NSNumber { | ||
| let size = sizeValue.intValue | ||
| if size > maxFileSizeBytes { | ||
| throw TraceFileError.fileTooLarge(url, actualBytes: size, maxBytes: maxFileSizeBytes) | ||
| } | ||
| let handle = try FileHandle(forReadingFrom: url) | ||
| defer { try? handle.close() } | ||
|
|
||
| let initialSize = try handle.seekToEnd() | ||
| if initialSize > UInt64(maxFileSizeBytes) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| let actualBytes = Int(initialSize > UInt64(Int.max) ? UInt64(Int.max) : initialSize) | ||
| throw TraceFileError.fileTooLarge(url, actualBytes: actualBytes, maxBytes: maxFileSizeBytes) | ||
| } | ||
| try handle.seek(toOffset: 0) | ||
|
|
||
| let data = try handle.read(upToCount: maxFileSizeBytes + 1) ?? Data() | ||
| if data.count > maxFileSizeBytes { | ||
| throw TraceFileError.fileTooLarge(url, actualBytes: data.count, maxBytes: maxFileSizeBytes) | ||
| } | ||
| return data | ||
| } catch let fileError as TraceFileError { | ||
| throw fileError | ||
| } catch { | ||
| throw TraceFileError.readFailed(url) | ||
| } | ||
|
|
||
| do { | ||
| return try Data(contentsOf: url) | ||
| } catch { | ||
| throw TraceFileError.readFailed(url) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
guard let selfcausesstreamResponseto fail withCancellationErrorif theTerraTracedSessioninstance is released before theTaskstarts, even thoughmodelIdentifierandsessionare already captured and sufficient to run the stream. This introduces a regression for call patterns that don't keep an extra strong reference (for example, consuming a stream returned from a temporary session), leading to nondeterministic early cancellation.Useful? React with 👍 / 👎.