c - 工具链(编译器/调试器)中的错误?

标签 c debugging gcc gdb x86-64

gdb、gcc 或两者都有严重错误,我不知道是什么。我创建了一个 POC 来在 gcc (GCC) 4.6.1 20110819(预发布)GNU gdb (GDB) 7.3.50.20110908-cvs 上重现它 - 是的,这个是CVS版本,因为我遇到了这个问题http://sourceware.org/bugzilla/show_bug.cgi?id=12435 ,但是我也使用 GNU gdb (GDB) 7.3.1 进行了测试,它的行为是相同的。

现在到 POC:https://gist.github.com/1211017问题可以在第33行观察到

在我看来,*c->foos[i] 应该求值为第 i 个 foo 的地址,如第 21 行所示。但是可执行文件崩溃了,如您在输出。

有人知道这是怎么回事吗?欢迎 ASM 级别或 GCC 内部的解释。

附录:

  1. 数据结构不允许更改 - 它们不属于我的代码,我必须与它们进行互操作。
  2. 代码 *c->foos[i] 用于与旧版本的 gcc 配合使用。
  3. dump_container 必须具有 container* 作为参数 - 此函数也不属于我的代码

还值得一提的是:*c->foos[i] 曾经有效。

最佳答案

通过永远有用的 valgrind 运行你的代码说:

==16066== Use of uninitialised value of size 8
==16066==    at 0x40061B: dump_container (bug.c:33)
==16066==    by 0x400726: main (bug.c:49)
==16066== 
==16066== Invalid read of size 4
==16066==    at 0x400620: dump_container (stdio2.h:105)
==16066==    by 0x400726: main (bug.c:49)
==16066==  Address 0x89485ed18949ed39 is not stack'd, malloc'd or (recently) free'd
==16066== 
==16066== 
==16066== Process terminating with default action of signal 11 (SIGSEGV)
==16066==  General Protection Fault
==16066==    at 0x400620: dump_container (stdio2.h:105)
==16066==    by 0x400726: main (bug.c:49)
==16066== 

My guess is that you'll find the bug in your own code rather than the compiler or debugger.

EDIT Or since you want me to spell it out for you, change line 33 to

printf("foo[%d]=%d at %p\n", i, (*c->foos)[i]->i, (void*) (*c->foos)[i]);

关于c - 工具链(编译器/调试器)中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387074/

相关文章:

c - c 中指针的类型转换

debugging - 如何启用 OpenEdge 调试器?

我可以让 gcc 在链接共享库时忽略静态库吗?

linux - 如何在 Linux 上禁用二进制文件的地址空间随机化?

c++ - GCC 规范文件 : how to get the installation path

c - 为什么我收到从 'double' 到 'float' 的转换,可能会丢失数据警告?(c)

c++ - 使用 C++ 获取在后台执行的命令的实际进程 ID

c - 如何使用 C 结构中的字符串字段?

c - 使用 vscode 在调试多线程 C 程序时单步切换线程

java - Android 如何处理控制台登录发布