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-2272] Crash while accessing CommandLine.arguments on macOS XCTest test cases #382

Closed
swift-ci opened this issue Aug 4, 2016 · 6 comments

Comments

@swift-ci
Copy link

swift-ci commented Aug 4, 2016

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

macOS

Additional Detail from JIRA
Votes 0
Component/s Standard Library, XCTest
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: a3168b64f3cb1c06e8605f267e75f494

Issue Description:

If you try to access CommandLine.arguments or CommandLine.unsafeArgv inside an XCTest test case you get nice crash:

fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0 libswiftCore.dylib 0x000000010d6e0370 swift_reportError + 125
1 libswiftCore.dylib 0x000000010d6fcf90 _swift_stdlib_reportFatalError + 61
2 libswiftCore.dylib 0x000000010d6a9fe0 partial apply for thunk + 63
3 libswiftCore.dylib 0x000000010d503c50 specialized StaticString.withUTF8Buffer<A> (invoke : (UnsafeBufferPointer<UInt8>) -> A) -> A + 351
4 libswiftCore.dylib 0x000000010d672320 partial apply for (_fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> ()).(closure #2) + 158
5 libswiftCore.dylib 0x000000010d6a9fe0 partial apply for thunk + 63
6 libswiftCore.dylib 0x000000010d6afe20 partial apply for thunk + 14
7 libswiftCore.dylib 0x000000010d503c50 specialized StaticString.withUTF8Buffer<A> (invoke : (UnsafeBufferPointer<UInt8>) -> A) -> A + 351
8 libswiftCore.dylib 0x000000010d632b40 specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> () + 143
9 libswiftCore.dylib 0x000000010d671a00 specialized static Process._computeArguments() -> [String] + 1071
10 libswiftCore.dylib 0x000000010d624850 static Process.arguments.getter + 149
11 QuarkTests 0x000000010cc612a0 loadConfiguration(configurationFile : String) throws -> StructuredData + 755
12 QuarkTests 0x000000010cc606d0 configure<A where ...> (configurationFile : String, server : ConfigurableServer.Type, configure : (A) throws -> ResponderRepresentable) -> () + 169
13 QuarkTests 0x000000010cc5bc70 QuarkTests.testConfiguration() throws -> () + 495
14 QuarkTests 0x000000010cc5c7e0 @objc QuarkTests.testConfiguration() throws -> () + 50
15 CoreFoundation 0x00007fff85dde9e0 invoking_ + 140
16 CoreFoundation 0x00007fff85dde7e0 -[NSInvocation invoke] + 286
17 XCTest 0x0000000109c62d7e __24-[XCTestCase invokeTest]_block_invoke_2 + 481
18 XCTest 0x0000000109c9bc54 -[XCTestContext performInScope:] + 190
19 XCTest 0x0000000109c62c6c -[XCTestCase invokeTest] + 255
20 XCTest 0x0000000109c6340e -[XCTestCase performTest:] + 457
21 XCTest 0x0000000109c608ac -[XCTestSuite performTest:] + 447
22 XCTest 0x0000000109c608ac -[XCTestSuite performTest:] + 447
23 XCTest 0x0000000109c608ac -[XCTestSuite performTest:] + 447
24 XCTest 0x0000000109c74bc1 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 602
25 XCTest 0x0000000109c9cf0f _XCTestMain + 1050
26 xctest 0x0000000109c4374b <unavailable> + 0
27 libdyld.dylib 0x00007fff9772f5ac start + 1

For some reason _NSGetArgv is returning NULL here:

https://github.com/apple/swift/blob/fe932663a8b9119636d8e61711df733f3c9f703e/stdlib/public/stubs/CommandLine.cpp#L58

That behaviour only happens on Linux.
I protected from the crash by doing

var commandLineArguments: [String] {
    return CommandLine.argc > 0 ? CommandLine.arguments : []
}

Maybe the same logic could be applied on the macOS shims. If this is the right direction I can send a PR with the fix. Another option would be to inspect exactly why XCTest is providing no command line arguments. But this would be much harder to do. Going the XCTest route or not I think crash is no good, so it should be protected somehow.

I think the correct behaviour in cases of undefined arguments:

CommandLine.argc = 0
CommandLine.unsafeArgv = nil
CommandLine.arguments = []

@belkadan

@belkadan
Copy link

belkadan commented Aug 4, 2016

Are you using master or Xcode 8b4 or some other version?

cc CodaFi (JIRA User)

@CodaFi
Copy link
Member

CodaFi commented Aug 5, 2016

I can't reproduce this, and it is not safe for us to assume the check that you've just written. If you have no command line arguments then the argument buffer will point to an empty array and we will not read from it. Seeing nil here is a serious thing we should continue to fatal error on.

@CodaFi
Copy link
Member

CodaFi commented Aug 5, 2016

Moreover, why is this crashing in Process instead of CommandLine?

@swift-ci
Copy link
Author

swift-ci commented Aug 5, 2016

Comment by Paulo Ricardo Lopes de Faria (JIRA)

Oh![]( The error first appeared at June 20th snapshot when it was still Process. I thought I tried a new snapshot. I did create the test but I was running the on the old snapshot directory. So I thought the error was still present, but it's not. Something fixed it, haha. Good)

@belkadan
Copy link

belkadan commented Aug 5, 2016

CodaFi (JIRA User) did a lot of good work here, so it's probably his doing. Thanks, CodaFi (JIRA User)!

@swift-ci
Copy link
Author

swift-ci commented Aug 5, 2016

Comment by Paulo Ricardo Lopes de Faria (JIRA)

Thanks! CodaFi (JIRA User) (:

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

No branches or pull requests

3 participants