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

Fix close error message #433

Merged
merged 2 commits into from
Nov 3, 2023
Merged

Conversation

neonichu
Copy link
Contributor

@neonichu neonichu commented Nov 2, 2023

From the macOS manpage:

Upon successful completion, a value of 0 is returned. Otherwise, a value
of -1 is returned and the global integer variable errno is set to
indicate the error.

I'm not certain whether the existing code may be there for cross-platform support, so I changed it to check for -1 and only then consult the global errno. This should preserve existing behavior if there's a cross-platform angle here.

rdar://117861543

From the macOS manpage:

> Upon successful completion, a value of 0 is returned.  Otherwise, a value
> of -1 is returned and the global integer variable errno is set to
> indicate the error.

I'm not certain whether the existing code may be there for cross-platform support, so I changed it to check for -1 and only then consult the global `errno`. This should preserve existing behavior if there's a cross-platform angle here.

rdar://117861543
@neonichu neonichu self-assigned this Nov 2, 2023
@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please smoke test

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please test

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please test

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please test windows

@tomerd
Copy link
Contributor

tomerd commented Nov 2, 2023

cc @weissi , does this looks right to you?

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

Error on repo "C:\Users\swift-ci\jenkins\workspace\swift-tools-support-core-PR-windows\swift": Traceback (most recent call last):
  File "C:\Users\swift-ci\jenkins\workspace\swift-tools-support-core-PR-windows\swift\utils\update_checkout\update_checkout\update_checkout.py", line 212, in update_single_repository
    result = shell.run(['git', 'rev-parse', checkout_target])
  File "C:\Users\swift-ci\jenkins\workspace\swift-tools-support-core-PR-windows\swift\utils\swift_build_support\swift_build_support\shell.py", line 257, in run
    raise eout
Exception: ['git', 'rev-parse', 'main']

During handling of the above exception, another exception occurred:

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please test windows

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

Looks like the Windows CI error is persistent, cc @shahmishal

@neonichu
Copy link
Contributor Author

neonichu commented Nov 2, 2023

@swift-ci please test windows

case .close(let err):
let errorMessage: String
if err == -1 { // if the return code is -1, we need to consult the global `errno`
errorMessage = strerror(errno)
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to copy this string immediately or else it's UB and you might get garbage or worse:

man strerror:

BUGS
For unknown error numbers, the strerror() function will return its result
in a static buffer which may be overwritten by subsequent calls.

So you want to save a String(cString: strerror(errno)) + " (\(errno))" or something. I'd say let's not lose the actual errno number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like we are basically doing that, the strerror here is not the libc function, but a local one https://github.com/apple/swift-tools-support-core/blob/main/Sources/TSCBasic/misc.swift#L320-L345

return "close error: \(strerror(errno))"
case .close(let err):
let errorMessage: String
if err == -1 { // if the return code is -1, we need to consult the global `errno`
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, errno is only guaranteed to overridden when the syscall fails, i.e. returns -1.

@neonichu neonichu merged commit 353c863 into swiftlang:main Nov 3, 2023
3 checks passed
@neonichu neonichu deleted the fix-close-error-message branch November 3, 2023 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants