【问题标题】:How to use gdb to debug out of bounds?如何使用gdb进行越界调试?
【发布时间】:2018-09-30 14:47:31
【问题描述】:

示例代码如下,如何使用gdb定位bug?

#include <stdio.h>

int main()  
{ 
   int arr[2]; 
   int a = 3;
   while(a>=0){
     arr[a]=a;
     a--;
   }

   return 1; 
} 

【问题讨论】:

    标签: c gdb indexoutofboundsexception


    【解决方案1】:

    如何使用 gdb 定位错误

    GDB 不是定位此错误的正确工具 -- Address Sanitizer 是(Address Sanitizer 会直接将您指向错误)。 p>

    如果您怀疑arr 上存在数组溢出,您可以在刚刚超过数组末尾的地址上设置一个观察点:

    (gdb) watch arr[3]
    

    当该地址的值改变时,GDB 停止。

    但这要求您已经知道您的程序中存在堆栈溢出,而您通常不知道。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 2015-06-13
      • 1970-01-01
      相关资源
      最近更新 更多