Valgrind 大小为 1 的无效读取 (sscanf)

Valgrind Invalid read of size 1 (sscanf)

Valgrind 以某种方式在我的程序的第一行显示错误:

int main(int argc, char** argv) {
  int i, r;
  sscanf(argv[1], "%d", &r);

  return 0;
}

Valgrind 报告:

==18674== Invalid read of size 1
==18674==    at 0x4ECB1A0: rawmemchr (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EB2F41: _IO_str_init_static_internal (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EA16C6: __isoc99_vsscanf (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EA1666: __isoc99_sscanf (in /usr/lib64/libc-2.23.so)
==18674==    by 0x400DE3: main (test_b_arbre.c:18)
==18674==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==18674== 
==18674== 
==18674== Process terminating with default action of signal 11 (SIGSEGV)
==18674==  Access not within mapped region at address 0x0
==18674==    at 0x4ECB1A0: rawmemchr (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EB2F41: _IO_str_init_static_internal (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EA16C6: __isoc99_vsscanf (in /usr/lib64/libc-2.23.so)
==18674==    by 0x4EA1666: __isoc99_sscanf (in /usr/lib64/libc-2.23.so)
==18674==    by 0x400DE3: main (test_b_arbre.c:18)

我遇到了一些类似的问题,但我没有找到解决方法... 我如何 运行 程序:

valgrind --leak-check=yes --track-origins=yes ./b_arbre 1 2 3 4 5 6

你在调用它时没有参数,所以 argv[1] 是一个空指针。 "Fix" 它通过提供命令行参数。通过检查 argc 并在它为 1.

时执行其他操作来正确修复它

我在 64 位 x86_64 Linux 上编译了你的确切程序(在你的 Valgrind 输出中看到了 64 位库的提示)。问题没有重现。我收到有关 sscanf 隐式声明不正确的警告,但这是一个转移注意力的问题。

我也试过 64 位 Power PC Linux。也清洁 Valgrind。

(当然,如果不带参数调用程序,则会发生空指针取消引用,在这种情况下 argv[argc] 已完成;但问题被描述为 with 参数。)

问题可能是正在测试的可执行文件与源代码不匹配。