Skip to content

Commit

Permalink
Android: add better nullability checks for nullability annotations ad…
Browse files Browse the repository at this point in the history
…ded in NDK 26 (#444) (#457)

Also fix one test.
  • Loading branch information
finagolfin authored May 11, 2024
1 parent 80d8813 commit 90bdc2a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Sources/TSCBasic/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,7 @@ private struct LocalFileSystem: FileSystem {

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

func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
// Open the file.
let fp = fopen(path.pathString, "wb")
if fp == nil {
guard let fp = fopen(path.pathString, "wb") else {
throw FileSystemError(errno: errno, path)
}
defer { fclose(fp) }
Expand Down
10 changes: 9 additions & 1 deletion Sources/TSCBasic/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public final class Process {

/// The current OS does not support the workingDirectory API.
case workingDirectoryNotSupported

/// The stdin could not be opened.
case stdinUnavailable
}

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

let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
guard let fp = fdopen(stdinPipe[1], "wb") else {
throw Process.Error.stdinUnavailable
}
let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true)

// Dupe the read portion of the remote to 0.
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
Expand Down Expand Up @@ -1258,6 +1264,8 @@ extension Process.Error: CustomStringConvertible {
return "could not find executable for '\(program)'"
case .workingDirectoryNotSupported:
return "workingDirectory is not supported in this platform"
case .stdinUnavailable:
return "could not open stdin on this platform"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TSCBasic/WritableByteStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public final class LocalFileOutputByteStream: FileOutputByteStream {
override final func writeImpl(_ bytes: ArraySlice<UInt8>) {
bytes.withUnsafeBytes { bytesPtr in
while true {
let n = fwrite(bytesPtr.baseAddress, 1, bytesPtr.count, filePointer)
let n = fwrite(bytesPtr.baseAddress!, 1, bytesPtr.count, filePointer)
if n < 0 {
if errno == EINTR { continue }
errorDetected(code: errno)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TSCTestSupport/PseudoTerminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class PseudoTerminal {
if openpty(&primary, &secondary, nil, nil, nil) != 0 {
return nil
}
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w"), closeOnDeinit: false) else {
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w")!, closeOnDeinit: false) else {
return nil
}
self.outStream = outStream
Expand Down
2 changes: 1 addition & 1 deletion Tests/TSCBasicTests/PathShimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WalkTests : XCTestCase {
var expected: [AbsolutePath] = [
"\(root)/usr",
"\(root)/bin",
"\(root)/xbin"
"\(root)/etc"
]
#else
let root = ""
Expand Down

0 comments on commit 90bdc2a

Please sign in to comment.