From c062beeb866a48cc9385c9ce04af0a39fa55b40c Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 31 Mar 2016 18:26:38 -0700 Subject: [PATCH] Update for SE-0055: Making pointer nullability explicit. See https://github.com/apple/swift/pull/1878. --- Sources/POSIX/fopen.swift | 5 +++-- Sources/POSIX/opendir.swift | 5 +++-- Sources/POSIX/popen.swift | 2 +- Sources/POSIX/system.swift | 15 ++++++++++----- Sources/PackageDescription/Package.swift | 11 ++++++++--- Sources/Utility/File.swift | 4 +--- Sources/Utility/POSIXConvenience.swift | 6 ++---- Sources/Utility/walk.swift | 10 +++++----- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Sources/POSIX/fopen.swift b/Sources/POSIX/fopen.swift index 407eeb55f35..28e53fe17ca 100644 --- a/Sources/POSIX/fopen.swift +++ b/Sources/POSIX/fopen.swift @@ -18,7 +18,8 @@ public enum FopenMode: String { } public func fopen(_ path: String, mode: FopenMode = .Read) throws -> UnsafeMutablePointer { - let f = libc.fopen(path, mode.rawValue) - guard f != nil else { throw SystemError.fopen(errno, path) } + guard let f = libc.fopen(path, mode.rawValue) else { + throw SystemError.fopen(errno, path) + } return f } diff --git a/Sources/POSIX/opendir.swift b/Sources/POSIX/opendir.swift index 6ba57e90c2b..03aed9e544f 100644 --- a/Sources/POSIX/opendir.swift +++ b/Sources/POSIX/opendir.swift @@ -13,8 +13,9 @@ import var libc.errno import func libc.opendir public func opendir(_ path: String) throws -> DirHandle { - let d = libc.opendir(path) - guard d != nil else { throw SystemError.opendir(errno, path) } + guard let d = libc.opendir(path) else { + throw SystemError.opendir(errno, path) + } return d } diff --git a/Sources/POSIX/popen.swift b/Sources/POSIX/popen.swift index 0a1f1217587..40963b27c30 100644 --- a/Sources/POSIX/popen.swift +++ b/Sources/POSIX/popen.swift @@ -31,7 +31,7 @@ public func popen(_ arguments: [String], redirectStandardError: Bool = false, en // Create the file actions to use for spawning. #if os(OSX) - var fileActions: posix_spawn_file_actions_t = nil + var fileActions: posix_spawn_file_actions_t? = nil #else var fileActions = posix_spawn_file_actions_t() #endif diff --git a/Sources/POSIX/system.swift b/Sources/POSIX/system.swift index e59821200b2..f948b021718 100644 --- a/Sources/POSIX/system.swift +++ b/Sources/POSIX/system.swift @@ -42,11 +42,16 @@ public func system(_ arguments: [String], environment: [String:String] = [:]) th public func system() {} +#if os(OSX) +typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t? +#else +typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t +#endif /// Convenience wrapper for posix_spawn. -func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: posix_spawn_file_actions_t? = nil) throws -> pid_t { - let argv = args.map{ $0.withCString(strdup) } - defer { for arg in argv { free(arg) } } +func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: swiftpm_posix_spawn_file_actions_t? = nil) throws -> pid_t { + let argv: [UnsafeMutablePointer?] = args.map{ $0.withCString(strdup) } + defer { for case let arg? in argv { free(arg) } } var environment = environment #if Xcode @@ -60,8 +65,8 @@ func posix_spawnp(_ path: String, args: [String], environment: [String: String] } } - let env = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) } - defer { env.forEach{ free($0) } } + let env: [UnsafeMutablePointer?] = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) } + defer { for case let arg? in env { free(arg) } } var pid = pid_t() let rv: Int32 diff --git a/Sources/PackageDescription/Package.swift b/Sources/PackageDescription/Package.swift index 3ff921eab8d..8b4547e9a69 100644 --- a/Sources/PackageDescription/Package.swift +++ b/Sources/PackageDescription/Package.swift @@ -69,9 +69,14 @@ public final class Package { // to get the interpreter to dump the package when attempting to load a // manifest. - if let fileNoOptIndex = Process.arguments.index(of: "-fileno"), - fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) { - dumpPackageAtExit(self, fileNo: fileNo) + // FIXME: Additional hackery here to avoid accessing 'arguments' in a + // process whose 'main' isn't generated by Swift. + // See https://bugs.swift.org/browse/SR-1119. + if Process.argc > 0 { + if let fileNoOptIndex = Process.arguments.index(of: "-fileno"), + fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) { + dumpPackageAtExit(self, fileNo: fileNo) + } } } } diff --git a/Sources/Utility/File.swift b/Sources/Utility/File.swift index f47b985e325..a14e914b455 100644 --- a/Sources/Utility/File.swift +++ b/Sources/Utility/File.swift @@ -56,9 +56,7 @@ public class FileLineGenerator: IteratorProtocol, Sequence { } deinit { - if fp != nil { - fclose(fp) - } + fclose(fp) } public func next() -> String? { diff --git a/Sources/Utility/POSIXConvenience.swift b/Sources/Utility/POSIXConvenience.swift index 2a53ebf9222..ac5377701f3 100644 --- a/Sources/Utility/POSIXConvenience.swift +++ b/Sources/Utility/POSIXConvenience.swift @@ -11,11 +11,9 @@ import POSIX public func fopen(_ path: String..., mode: FopenMode = .Read, body: (UnsafeMutablePointer) throws -> Void) throws { - var fp = try POSIX.fopen(Path.join(path), mode: mode) - defer { if fp != nil { fclose(fp) } } + let fp = try POSIX.fopen(Path.join(path), mode: mode) + defer { fclose(fp) } try body(fp) - fclose(fp) // defer is not necessarily immediate - fp = nil } @_exported import func POSIX.fputs diff --git a/Sources/Utility/walk.swift b/Sources/Utility/walk.swift index ff838895930..9c71bdbc183 100644 --- a/Sources/Utility/walk.swift +++ b/Sources/Utility/walk.swift @@ -62,7 +62,7 @@ public func walk(_ paths: String..., recursing: (String) -> Bool) -> RecursibleD A generator for a single directory’s contents */ private class DirectoryContentsGenerator: IteratorProtocol { - private let dirptr: DirHandle + private let dirptr: DirHandle? private let path: String private init(path: String) { @@ -72,16 +72,16 @@ private class DirectoryContentsGenerator: IteratorProtocol { } deinit { - if dirptr != nil { closedir(dirptr) } + if let openeddir = dirptr { closedir(openeddir) } } func next() -> dirent? { - if dirptr == nil { return nil } // yuck, silently ignoring the error to maintain this pattern + guard let validdir = dirptr else { return nil } // yuck, silently ignoring the error to maintain this pattern while true { var entry = dirent() - var result: UnsafeMutablePointer = nil - guard readdir_r(dirptr, &entry, &result) == 0 else { continue } + var result: UnsafeMutablePointer? = nil + guard readdir_r(validdir, &entry, &result) == 0 else { continue } guard result != nil else { return nil } switch (Int32(entry.d_type), entry.d_name.0, entry.d_name.1, entry.d_name.2) {