1

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;
}
4

1 回答 1

2

根据您的代码,ssock是一个整数,一个文件描述符,因为您close()在默认情况下(父级)将其传递给。

但是,您还将该整数作为字符指针传递execve给参数列表(在子情况下)。这样的整数不太可能映射到有效的内存地址,因此您的错误。事实上,即使它确实映射到一个有效的地址,程序很可能无论如何都会崩溃,因为该地址可能不包含有效的字符串;至少,你会得到意想不到的结果。

于 2014-03-09T19:36:23.927 回答