Skip to content

Android: add better nullability checks for nullability annotations added in NDK 26 #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Sources/TSCBasic/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,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 @@ -520,8 +519,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/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,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 @@ -697,7 +700,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 @@ -1376,6 +1382,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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This force-unwrap works fine on linux too, but I separated it out anyway in case there is concern it breaks other platforms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this done elsewhere in this repo so should be fine, doing the force unwrap on all platforms now.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArraySlice currently never passes a buffer with a nil baseAddress. It's arguably an implementation detail, but we rely on it in many places.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I only modified this because the compiler complained and errored. Is there something you'd like me to change here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just wanted to further clarify why the force unwrap is correct here.

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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't bother separating this out into a guard let since it's just for the tests, can do so if wanted.

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