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

libc::execv incorrect definition #16290

Closed
carllerche opened this issue Aug 6, 2014 · 6 comments
Closed

libc::execv incorrect definition #16290

carllerche opened this issue Aug 6, 2014 · 6 comments

Comments

@carllerche
Copy link
Member

It should be:

pub unsafe fn execv(prog: *const c_char, argv: *const *const c_char) -> c_int
@mahkoh
Copy link
Contributor

mahkoh commented Aug 6, 2014

char *const argv[] == char *const *argv in function signatures.

#include <stdio.h>

#define typename(x) _Generic((x), \
    char *const *:      "char *const *", \
    default:            "unreachable!()")

void f(char *const x[]) {
    puts(typename(x));
}

int main(void) {
    f(0);
}

(only works in clang)

@mahkoh
Copy link
Contributor

mahkoh commented Aug 6, 2014

But the definition is still wrong. char *const * is a pointer to constant pointers to mutable characters. I think the following definition is correct.

pub unsafe fn execv(prog: *const c_char, argv: *const *mut c_char) -> c_int

@mahkoh
Copy link
Contributor

mahkoh commented Aug 6, 2014

Interestingly enough, the example in POSIX 2008 disregards the inner mutability in its execv example:

#include <unistd.h>

int main(void) {
    char *const cmd[] = { "ls", "-l", (char *)0 };
    execv("/bin/ls", cmd);
}
test.c:4:24: warning: initializing 'char *const' with an expression of type
      'const char [3]' discards qualifiers
      [-Wincompatible-pointer-types-discards-qualifiers]
        char *const cmd[] = { "ls", "-l", (char *)0 };
                              ^~~~
test.c:4:30: warning: initializing 'char *const' with an expression of type
      'const char [3]' discards qualifiers
      [-Wincompatible-pointer-types-discards-qualifiers]
        char *const cmd[] = { "ls", "-l", (char *)0 };
                                    ^~~~

I'm not quite sure what to believe.

@mahkoh
Copy link
Contributor

mahkoh commented Aug 7, 2014

A thorough reading of POSIX 2008 reveals that execv will never modify any level of the argv parameter. See the following screenshot for a detailed explanation (compatibility with old code).
2014-08-07-021239_594x414_scrot
So @carllerche's definition is correct after all.

@steveklabnik
Copy link
Member

Today, this is

                pub fn execv(prog: *const c_char,
                             argv: *mut *const c_char) -> intptr_t;

so it appears that it's still wrong.

@geofft
Copy link
Contributor

geofft commented Jun 20, 2015

PR #25641 should fix this. I didn't notice this issue at the time, but I reached the same conclusions about the actual const guarantees (and C's legacy problems therewith).

@bors bors closed this as completed in 058a0f0 Jun 21, 2015
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 8, 2024
internal: Reduce vec cloning in mir lowering/eval
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

No branches or pull requests

4 participants