Skip to content

Commit 442f62a

Browse files
committed
Update for SE-0055: Making pointer nullability explicit.
See swiftlang/swift#1878.
1 parent 903309e commit 442f62a

File tree

8 files changed

+33
-25
lines changed

8 files changed

+33
-25
lines changed

Sources/POSIX/fopen.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public enum FopenMode: String {
1818
}
1919

2020
public func fopen(_ path: String, mode: FopenMode = .Read) throws -> UnsafeMutablePointer<FILE> {
21-
let f = libc.fopen(path, mode.rawValue)
22-
guard f != nil else { throw SystemError.fopen(errno, path) }
21+
guard let f = libc.fopen(path, mode.rawValue) else {
22+
throw SystemError.fopen(errno, path)
23+
}
2324
return f
2425
}

Sources/POSIX/opendir.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import var libc.errno
1313
import func libc.opendir
1414

1515
public func opendir(_ path: String) throws -> DirHandle {
16-
let d = libc.opendir(path)
17-
guard d != nil else { throw SystemError.opendir(errno, path) }
16+
guard let d = libc.opendir(path) else {
17+
throw SystemError.opendir(errno, path)
18+
}
1819
return d
1920
}
2021

Sources/POSIX/popen.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public func popen(_ arguments: [String], redirectStandardError: Bool = false, en
3131

3232
// Create the file actions to use for spawning.
3333
#if os(OSX)
34-
var fileActions: posix_spawn_file_actions_t = nil
34+
var fileActions: posix_spawn_file_actions_t? = nil
3535
#else
3636
var fileActions = posix_spawn_file_actions_t()
3737
#endif

Sources/POSIX/system.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ public func system(_ arguments: [String], environment: [String:String] = [:]) th
4242
public func system() {}
4343

4444

45+
#if os(OSX)
46+
typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
47+
#else
48+
typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t
49+
#endif
4550

4651
/// Convenience wrapper for posix_spawn.
47-
func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: posix_spawn_file_actions_t? = nil) throws -> pid_t {
48-
let argv = args.map{ $0.withCString(strdup) }
49-
defer { for arg in argv { free(arg) } }
52+
func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: swiftpm_posix_spawn_file_actions_t? = nil) throws -> pid_t {
53+
let argv: [UnsafeMutablePointer<CChar>?] = args.map{ $0.withCString(strdup) }
54+
defer { for case let arg? in argv { free(arg) } }
5055

5156
var environment = environment
5257
#if Xcode
@@ -60,8 +65,8 @@ func posix_spawnp(_ path: String, args: [String], environment: [String: String]
6065
}
6166
}
6267

63-
let env = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) }
64-
defer { env.forEach{ free($0) } }
68+
let env: [UnsafeMutablePointer<CChar>?] = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) }
69+
defer { for case let arg? in env { free(arg) } }
6570

6671
var pid = pid_t()
6772
let rv: Int32

Sources/PackageDescription/Package.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ public final class Package {
6969
// to get the interpreter to dump the package when attempting to load a
7070
// manifest.
7171

72-
if let fileNoOptIndex = Process.arguments.index(of: "-fileno"),
73-
fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) {
74-
dumpPackageAtExit(self, fileNo: fileNo)
72+
// FIXME: Additional hackery here to avoid accessing 'arguments' in a
73+
// process whose 'main' isn't generated by Swift.
74+
// See https://bugs.swift.org/browse/SR-1119.
75+
if Process.argc > 0 {
76+
if let fileNoOptIndex = Process.arguments.index(of: "-fileno"),
77+
fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) {
78+
dumpPackageAtExit(self, fileNo: fileNo)
79+
}
7580
}
7681
}
7782
}

Sources/Utility/File.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public class FileLineGenerator: IteratorProtocol, Sequence {
5656
}
5757

5858
deinit {
59-
if fp != nil {
60-
fclose(fp)
61-
}
59+
fclose(fp)
6260
}
6361

6462
public func next() -> String? {

Sources/Utility/POSIXConvenience.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
import POSIX
1212

1313
public func fopen(_ path: String..., mode: FopenMode = .Read, body: (UnsafeMutablePointer<FILE>) throws -> Void) throws {
14-
var fp = try POSIX.fopen(Path.join(path), mode: mode)
15-
defer { if fp != nil { fclose(fp) } }
14+
let fp = try POSIX.fopen(Path.join(path), mode: mode)
15+
defer { fclose(fp) }
1616
try body(fp)
17-
fclose(fp) // defer is not necessarily immediate
18-
fp = nil
1917
}
2018

2119
@_exported import func POSIX.fputs

Sources/Utility/walk.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public func walk(_ paths: String..., recursing: (String) -> Bool) -> RecursibleD
6262
A generator for a single directory’s contents
6363
*/
6464
private class DirectoryContentsGenerator: IteratorProtocol {
65-
private let dirptr: DirHandle
65+
private let dirptr: DirHandle?
6666
private let path: String
6767

6868
private init(path: String) {
@@ -72,16 +72,16 @@ private class DirectoryContentsGenerator: IteratorProtocol {
7272
}
7373

7474
deinit {
75-
if dirptr != nil { closedir(dirptr) }
75+
if let openeddir = dirptr { closedir(openeddir) }
7676
}
7777

7878
func next() -> dirent? {
79-
if dirptr == nil { return nil } // yuck, silently ignoring the error to maintain this pattern
79+
guard let validdir = dirptr else { return nil } // yuck, silently ignoring the error to maintain this pattern
8080

8181
while true {
8282
var entry = dirent()
83-
var result: UnsafeMutablePointer<dirent> = nil
84-
guard readdir_r(dirptr, &entry, &result) == 0 else { continue }
83+
var result: UnsafeMutablePointer<dirent>? = nil
84+
guard readdir_r(validdir, &entry, &result) == 0 else { continue }
8585
guard result != nil else { return nil }
8686

8787
switch (Int32(entry.d_type), entry.d_name.0, entry.d_name.1, entry.d_name.2) {

0 commit comments

Comments
 (0)