Question

As the title suggests, I'm getting a "Bad Address" error when calling execve(). Whenever I found someone else having the same problem, they were just omitting the NULLs at the end of the arrays, but I'm doing that here and I'm still getting the error. Anybody know what could be causing it?

switch(fork()) {
    case 0: //child
        (void) close(msock);
        char *argv[] = {"./echo", (char *)ssock, NULL};
        char *envp[] = {NULL};
        int result = execve(argv[0], argv, envp);
        if (result < 0) {
            err_sys("execve");
        }
    case -1: //error
        err_sys("fork");
        break;
    default: //parent
        (void) close(ssock);
        break;
}
Was it helpful?

Solution

Based on your code, ssock is an integer, a file descriptor, since you are passing it to close() in the default case (parent).

However, you are also passing that integer as a character pointer to execve in the argument list (in the child case). It is unlikely that such an integer will map to a valid memory address, hence your error. In fact, even if it did map to a valid address, the program would most likely crash anyway since that address would probably not contain a valid string; at the very least, you would get unexpected results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top