Skip to content

liblibc: Prototype getopt on Linux as taking a mutable argument #25642

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

Closed
wants to merge 2 commits into from

Conversation

geofft
Copy link
Contributor

@geofft geofft commented May 20, 2015

[This is a followup PR to #25641 and contains that commit: this PR only proposes the second commit that GitHub lists.]

There's a GNU-specific weirdness in its getopt implementation (presumably assuming that the function will be called with a mutable argv from main). Quoting the "conforming to" section of its manpage:

POSIX.2 and POSIX.1-2001, provided the environment variable POSIXLY_CORRECT is set. Otherwise, the elements of argv aren't really const, because we permute them.

Since we have no way to enforce that that variable is set in an FFI binding, I think the safe thing to do is to mark the function as taking a mutable array.

I'm not sure if this is really the right approach. Should we change the other getopt prototypes to match, for consistency of compilation? Should we exempt other Linux libcs like Musl?

geofft added 2 commits May 20, 2015 03:24
These functions do not modify their arguments, so they does not need
mutable pointers. The C prototypes take a constant array of mutable
C-strings, but that's a legacy quirk from before C had const (since
string literals have type `char *`). The Rust prototypes had `*mut` in
the wrong place, anyway: to match the C prototypes, it should have been
`*const *mut c_char`.

Also fix the one caller of execvp in libstd, which now no longer needs
an unsafe cast.
This is a GNU-specific weirdness in its getopt implementation
(presumably assuming that the function will be called with a mutable
argv from main). Quoting the "conforming to" section of its manpage:
"POSIX.2 and POSIX.1-2001, provided the environment variable
POSIXLY_CORRECT is set. Otherwise, the elements of argv aren't really
const, because we permute them."

Since we have no way to enforce that that variable is set in an FFI
binding, the safe thing to do is to mark the function as taking a mutable
array.
@rust-highfive
Copy link
Contributor

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 27, 2015
@geofft
Copy link
Contributor Author

geofft commented Jun 20, 2015

Squashed into PR #25641.

@geofft geofft closed this Jun 20, 2015
@geofft geofft deleted the getopt-const branch June 20, 2015 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants