运行以下 C 程序,valgrind --leak-check=yes结果 valgrind 给出一个输出,表明
Syscall param execve(argv) points to unaddressable byte(s)
Run Code Online (Sandbox Code Playgroud)
程序如下:
int main() {
const int NUM_ARGS = 3;
char** run_arguments = malloc(sizeof(char*)*NUM_ARGS);
run_arguments[0] = "ls";
run_arguments[1] = "-l";
run_arguments[2] = "--color";
char* full_path = "/bin/ls";
int pid = fork();
if (pid == 0)
execv(full_path,run_arguments);
else {
int status;
waitpid(pid,&status,WUNTRACED);
free(run_arguments);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按照valgrind的说法,问题出在线上execv(full_path,run_arguments);,问题源于malloc线上的done char** run_arguments = malloc(sizeof(char*)*NUM_ARGS);。
我犯了什么错误导致 valgrind 给出这个输出?