Skip to content

Commit 90bdc2a

Browse files
authored
Android: add better nullability checks for nullability annotations added in NDK 26 (#444) (#457)
Also fix one test.
1 parent 80d8813 commit 90bdc2a

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

Sources/TSCBasic/FileSystem.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ private struct LocalFileSystem: FileSystem {
493493

494494
func readFileContents(_ path: AbsolutePath) throws -> ByteString {
495495
// Open the file.
496-
let fp = fopen(path.pathString, "rb")
497-
if fp == nil {
496+
guard let fp = fopen(path.pathString, "rb") else {
498497
throw FileSystemError(errno: errno, path)
499498
}
500499
defer { fclose(fp) }
@@ -523,8 +522,7 @@ private struct LocalFileSystem: FileSystem {
523522

524523
func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
525524
// Open the file.
526-
let fp = fopen(path.pathString, "wb")
527-
if fp == nil {
525+
guard let fp = fopen(path.pathString, "wb") else {
528526
throw FileSystemError(errno: errno, path)
529527
}
530528
defer { fclose(fp) }

Sources/TSCBasic/Process.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public final class Process {
144144

145145
/// The current OS does not support the workingDirectory API.
146146
case workingDirectoryNotSupported
147+
148+
/// The stdin could not be opened.
149+
case stdinUnavailable
147150
}
148151

149152
public enum OutputRedirection {
@@ -677,7 +680,10 @@ public final class Process {
677680
var stdinPipe: [Int32] = [-1, -1]
678681
try open(pipe: &stdinPipe)
679682

680-
let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
683+
guard let fp = fdopen(stdinPipe[1], "wb") else {
684+
throw Process.Error.stdinUnavailable
685+
}
686+
let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true)
681687

682688
// Dupe the read portion of the remote to 0.
683689
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
@@ -1258,6 +1264,8 @@ extension Process.Error: CustomStringConvertible {
12581264
return "could not find executable for '\(program)'"
12591265
case .workingDirectoryNotSupported:
12601266
return "workingDirectory is not supported in this platform"
1267+
case .stdinUnavailable:
1268+
return "could not open stdin on this platform"
12611269
}
12621270
}
12631271
}

Sources/TSCBasic/WritableByteStream.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ public final class LocalFileOutputByteStream: FileOutputByteStream {
790790
override final func writeImpl(_ bytes: ArraySlice<UInt8>) {
791791
bytes.withUnsafeBytes { bytesPtr in
792792
while true {
793-
let n = fwrite(bytesPtr.baseAddress, 1, bytesPtr.count, filePointer)
793+
let n = fwrite(bytesPtr.baseAddress!, 1, bytesPtr.count, filePointer)
794794
if n < 0 {
795795
if errno == EINTR { continue }
796796
errorDetected(code: errno)

Sources/TSCTestSupport/PseudoTerminal.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class PseudoTerminal {
2424
if openpty(&primary, &secondary, nil, nil, nil) != 0 {
2525
return nil
2626
}
27-
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w"), closeOnDeinit: false) else {
27+
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w")!, closeOnDeinit: false) else {
2828
return nil
2929
}
3030
self.outStream = outStream

Tests/TSCBasicTests/PathShimTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class WalkTests : XCTestCase {
3939
var expected: [AbsolutePath] = [
4040
"\(root)/usr",
4141
"\(root)/bin",
42-
"\(root)/xbin"
42+
"\(root)/etc"
4343
]
4444
#else
4545
let root = ""

0 commit comments

Comments
 (0)