Skip to content

Commit

Permalink
Fix bad file descriptor crashes (#97)
Browse files Browse the repository at this point in the history
Explicitly close the pipe file handle after the data has been read.
  • Loading branch information
kuhnroyal authored Nov 14, 2024
1 parent d69f366 commit 178cdd6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Packages/Keychain/Sources/Keychain/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public extension Keychain {
guard let data = read(Data.self, usingQuery: query.rawQuery) else {
return nil
}
return String(decoding: data, as: UTF8.self)
return String(data: data, encoding: .utf8)
}

func removePassword(forAccount account: String, belongingToService service: String) {
Expand Down
4 changes: 3 additions & 1 deletion Packages/Keychain/Sources/Keychain/RSAPrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public final class RSAPrivateKey {
}

public convenience init?(_ data: Data) {
let string = String(decoding: data, as: UTF8.self)
guard let string = String(data: data, encoding: .utf8) else {
return nil
}
self.init(string)
}

Expand Down
7 changes: 5 additions & 2 deletions Packages/Shell/Sources/ShellData/ProcessShell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public struct ProcessShell: Shell {
process.launchPath = executablePath
process.standardInput = nil
process.environment = environment
process.launch()
try process.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
// Explicitly close the pipe file handle to prevent running out of file descriptors.
// See https://github.com/swiftlang/swift/issues/57827
try pipe.fileHandleForReading.close()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw ProcessShellError.unexpectedTerminationStatus(process.terminationStatus)
}
return String(decoding: data, as: UTF8.self)
return String(data: data, encoding: .utf8) ?? ""
} onCancel: {
if sendableProcess.process.isRunning {
sendableProcess.process.terminate()
Expand Down
2 changes: 1 addition & 1 deletion xcconfigs/General.xcconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GENERATE_INFOPLIST_FILE = YES
CURRENT_PROJECT_VERSION = 1
MARKETING_VERSION = 0.9.0
MARKETING_VERSION = 0.10.1
DEVELOPMENT_TEAM = 566MC7D8D4
CODE_SIGN_STYLE = Automatic
CODE_SIGN_ENTITLEMENTS = Tartelet/Supporting files/Tartelet.entitlements
Expand Down

0 comments on commit 178cdd6

Please sign in to comment.