【发布时间】:2017-03-30 08:15:20
【问题描述】:
使用valgrind --leak-check=yes 运行以下 C 程序会导致 valgrind 给出指示
Syscall param execve(argv) points to unaddressable byte(s)
程序如下:
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;
}
根据valgrind的说法,问题出现在execv(full_path,run_arguments);线上,问题源于char** run_arguments = malloc(sizeof(char*)*NUM_ARGS);线上做的malloc。
我犯了什么错误导致 valgrind 给出这个输出?
【问题讨论】: