在 valgrind 中获取 "Syscall param execve(argv) points to unaddressable byte(s)"
Getting "Syscall param execve(argv) points to unaddressable byte(s)" in valgrind
运行 以下带有 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);
行,问题源于malloc
行char** run_arguments = malloc(sizeof(char*)*NUM_ARGS);
。
我犯了什么错误导致 valgrind 给出这个输出?
参数列表必须由 NULL
指针终止。向 run_arguments
数组添加一个元素,并使其成为 NULL
指针。
如果没有空指针参数,exec
函数将在搜索终止符时越界,并将每个非空元素视为应传递给程序的参数。这会导致 未定义的行为 。
这在 the exec
manual page 中有明确说明。
运行 以下带有 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);
行,问题源于malloc
行char** run_arguments = malloc(sizeof(char*)*NUM_ARGS);
。
我犯了什么错误导致 valgrind 给出这个输出?
参数列表必须由 NULL
指针终止。向 run_arguments
数组添加一个元素,并使其成为 NULL
指针。
如果没有空指针参数,exec
函数将在搜索终止符时越界,并将每个非空元素视为应传递给程序的参数。这会导致 未定义的行为 。
这在 the exec
manual page 中有明确说明。