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

标签 c pointers valgrind scanf

不知何故,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

最佳答案

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

我还尝试了 64 位 Power PC Linux。也清洁 Valgrind。

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

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

关于c - Valgrind 大小为 1 的无效读取 (sscanf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44641971/

相关文章:

c - 使用指向结构的指针或引用

c - 结构体上的 free() 无效

c - 如何让 valgrind 报告 "where"发生 "definitely lost"

c++ - static_cast 可以将非空指针转换为空指针吗?

c - 函数运行后不打印

c - 没有回调的 Nanopb

c - 如何从合法的未知值类型中提取标准注册表值类型?

Python 对对象的引用以节省额外的输入

c++ - Valgrind 无效读取大小 8 C++

c - ORA-01031 : insufficient privileges on insert