Skip to content

Commit 56604c0

Browse files
committed
Android: add better nullability checks for nullability annotations added in NDK 26
Also fix one test.
1 parent 0583d26 commit 56604c0

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
@@ -490,8 +490,7 @@ private struct LocalFileSystem: FileSystem {
490490

491491
func readFileContents(_ path: AbsolutePath) throws -> ByteString {
492492
// Open the file.
493-
let fp = fopen(path.pathString, "rb")
494-
if fp == nil {
493+
guard let fp = fopen(path.pathString, "rb") else {
495494
throw FileSystemError(errno: errno, path)
496495
}
497496
defer { fclose(fp) }
@@ -520,8 +519,7 @@ private struct LocalFileSystem: FileSystem {
520519

521520
func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
522521
// Open the file.
523-
let fp = fopen(path.pathString, "wb")
524-
if fp == nil {
522+
guard let fp = fopen(path.pathString, "wb") else {
525523
throw FileSystemError(errno: errno, path)
526524
}
527525
defer { fclose(fp) }

Sources/TSCBasic/Process/Process.swift

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

192192
/// The current OS does not support the workingDirectory API.
193193
case workingDirectoryNotSupported
194+
195+
/// The stdin could not be opened.
196+
case stdinUnavailable
194197
}
195198

196199
public enum OutputRedirection {
@@ -697,7 +700,10 @@ public final class Process {
697700
var stdinPipe: [Int32] = [-1, -1]
698701
try open(pipe: &stdinPipe)
699702

700-
let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
703+
guard let fp = fdopen(stdinPipe[1], "wb") else {
704+
throw Process.Error.stdinUnavailable
705+
}
706+
let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true)
701707

702708
// Dupe the read portion of the remote to 0.
703709
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
@@ -1376,6 +1382,8 @@ extension Process.Error: CustomStringConvertible {
13761382
return "could not find executable for '\(program)'"
13771383
case .workingDirectoryNotSupported:
13781384
return "workingDirectory is not supported in this platform"
1385+
case .stdinUnavailable:
1386+
return "could not open stdin on this platform"
13791387
}
13801388
}
13811389
}

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)