Skip to content
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

[SR-11405] Can't build Swift PM due to non-unwrapped arguments to C function calls #4668

Closed
swift-ci opened this issue Aug 31, 2019 · 7 comments
Labels

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-11405
Radar None
Original Reporter ryanw (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Fedora 31 Branched x86_64

Swift master @Head (a951a4) with @tachoknight's Python 3 patch cherry-picked.

Additional Detail from JIRA
Votes 0
Component/s Package Manager
Labels Bug
Assignee None
Priority Medium

md5: 004490f8efd8e192ac0f2157161c738e

Issue Description:

I'm trying to build a Swift toolchain from master on Linux (Fedora 31) but the SwiftPM build fails with the following message:

Compiling Swift Module 'Basic' (41 sources)
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:372:59: error: value of optional type 'UnsafeMutablePointer<Int8>?' must be unwrapped to a value of type 'UnsafeMutablePointer<Int8>'
        posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)
                                                          ^
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:372:59: note: coalesce using '??' to provide a default when the optional value contains 'nil'
        posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)
                                                          ^
                                                                  ?? <#default value#>
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:372:59: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
        posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)
                                                          ^
                                                                 !
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:395:54: error: value of optional type 'UnsafeMutablePointer<Int8>?' must be unwrapped to a value of type 'UnsafeMutablePointer<Int8>'
        let rv = posix_spawnp(&processID, argv.cArray[0], &fileActions, &attributes, argv.cArray, env.cArray)
                                                     ^
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:395:54: note: coalesce using '??' to provide a default when the optional value contains 'nil'
        let rv = posix_spawnp(&processID, argv.cArray[0], &fileActions, &attributes, argv.cArray, env.cArray)
                                                     ^
                                                         ?? <#default value#>
/home/ryan/Projects/Develop/swift-source/swiftpm/Sources/Basic/Process.swift:395:54: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
        let rv = posix_spawnp(&processID, argv.cArray[0], &fileActions, &attributes, argv.cArray, env.cArray)
                                                     ^
                                                        !
--- bootstrap: error: build failed with exit status 1
./utils/build-script: fatal error: command terminated with a non-zero exit status 1, aborting
./utils/build-script: fatal error: command terminated with a non-zero exit status 1, aborting
@belkadan
Copy link

belkadan commented Sep 3, 2019

Possibly a difference between Ubuntu and Fedora C stdlibs?

@swift-ci
Copy link
Contributor Author

Comment by Ryan (JIRA)

ubuntu 18.04 seems to be using glibc 2.27, and Fedora 31 currently has 2.30, but the functions don't appear to have changed between those two versions.

@swift-ci
Copy link
Contributor Author

Comment by Jonas Schwartz (JIRA)

I am seeing the same error on Arch Linux, also glibc 2.30

I am trying to compile the latest Swift 5.1.1-RELEASE, anyone have an idea for a solution for this?

@swift-ci
Copy link
Contributor Author

Comment by Ryan (JIRA)

I'm not proud, but

diff --git a/Sources/TSCBasic/Process.swift b/Sources/TSCBasic/Process.swift
index 9d53054d..8a817f0d 100644
--- a/Sources/TSCBasic/Process.swift
+++ b/Sources/TSCBasic/Process.swift
@@ -366,7 +366,9 @@ public final class Process: ObjectIdentifierProtocol {
         defer { posix_spawn_file_actions_destroy(&fileActions) }

         // Workaround for https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=89e435f3559c53084498e9baad22172b64429362
-        let devNull = strdup("/dev/null")
+        guard let devNull = strdup("/dev/null") else {
+            throw SystemError.posix_spawn(0, arguments)
+        }
         defer { free(devNull) }
         // Open /dev/null as stdin.
         posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)
@@ -392,7 +394,7 @@ public final class Process: ObjectIdentifierProtocol {

         let argv = CStringArray(arguments)
         let env = CStringArray(environment.map({ "\($0.0)=\($0.1)" }))
-        let rv = posix_spawnp(&processID, argv.cArray[0], &fileActions, &attributes, argv.cArray, env.cArray)
+        let rv = posix_spawnp(&processID, argv.cArray[0]!, &fileActions, &attributes, argv.cArray, env.cArray)

         guard rv == 0 else {
             throw SystemError.posix_spawn(rv, arguments)

@swift-ci
Copy link
Contributor Author

Comment by Jonas Schwartz (JIRA)

ryanw (JIRA User) thanks, was thinking the same, something in the back of my head just screamed no, but then again it's just my local dev machine 🙂

@belkadan
Copy link

You're welcome to make it both-ways compatible and put up a PR. Just leave comments as to why it's necessary!

guard let devNull = strdup("/dev/null") as Optional else {
(argv.cArray[0] as Optional)!

@swift-ci
Copy link
Contributor Author

Comment by Ryan (JIRA)

Fixed on Fedora 31 by swift-pm@35e66e5.

Thanks jonas (JIRA User)& @aciidb0mb3r!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 4, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants