小编fak*_*dad的帖子

在 valgrind 中获取“系统调用参数 execve(argv) 指向无法寻址的字节”

运行以下 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 给出这个输出?

c valgrind

3
推荐指数
1
解决办法
5142
查看次数

标签 统计

c ×1

valgrind ×1