Skip to content

Commit 197ab94

Browse files
committed
Android: add better nullability checks for nullability annotations added in NDK 26
1 parent 4d539ff commit 197ab94

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Sources/TSCBasic/FileSystem.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ private struct LocalFileSystem: FileSystem {
491491

492492
func readFileContents(_ path: AbsolutePath) throws -> ByteString {
493493
// Open the file.
494-
let fp = fopen(path.pathString, "rb")
495-
if fp == nil {
494+
let fpo = fopen(path.pathString, "rb")
495+
guard let fp = fpo else {
496496
throw FileSystemError(errno: errno, path)
497497
}
498498
defer { fclose(fp) }
@@ -521,8 +521,8 @@ private struct LocalFileSystem: FileSystem {
521521

522522
func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
523523
// Open the file.
524-
let fp = fopen(path.pathString, "wb")
525-
if fp == nil {
524+
let fpo = fopen(path.pathString, "wb")
525+
guard let fp = fpo else {
526526
throw FileSystemError(errno: errno, path)
527527
}
528528
defer { fclose(fp) }

Sources/TSCBasic/Process.swift

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

148148
/// The current OS does not support the workingDirectory API.
149149
case workingDirectoryNotSupported
150+
151+
/// The stdin could not be opened.
152+
case stdinNotOpening
150153
}
151154

152155
public enum OutputRedirection {
@@ -680,7 +683,10 @@ public final class Process {
680683
var stdinPipe: [Int32] = [-1, -1]
681684
try open(pipe: &stdinPipe)
682685

683-
let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
686+
guard let fp = fdopen(stdinPipe[1], "wb") else {
687+
throw Process.Error.stdinNotOpening
688+
}
689+
let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true)
684690

685691
// Dupe the read portion of the remote to 0.
686692
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
@@ -1265,6 +1271,8 @@ extension Process.Error: CustomStringConvertible {
12651271
return "could not find executable for '\(program)'"
12661272
case .workingDirectoryNotSupported:
12671273
return "workingDirectory is not supported in this platform"
1274+
case .stdinNotOpening:
1275+
return "could not open stdin on this platform"
12681276
}
12691277
}
12701278
}

Sources/TSCBasic/WritableByteStream.swift

+4
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,11 @@ public final class LocalFileOutputByteStream: FileOutputByteStream {
790790
override final func writeImpl(_ bytes: ArraySlice<UInt8>) {
791791
bytes.withUnsafeBytes { bytesPtr in
792792
while true {
793+
#if os(Android)
794+
let n = fwrite(bytesPtr.baseAddress!, 1, bytesPtr.count, filePointer)
795+
#else
793796
let n = fwrite(bytesPtr.baseAddress, 1, bytesPtr.count, filePointer)
797+
#endif
794798
if n < 0 {
795799
if errno == EINTR { continue }
796800
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

0 commit comments

Comments
 (0)