美文网首页
GDB 难以定位的 Crash - 越界访问

GDB 难以定位的 Crash - 越界访问

作者: tarzipc | 来源:发表于2018-05-22 23:16 被阅读0次

示例代码:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

void Funct() {
    map<int, int> iimap;

    iimap[1] = 1;
}


int main(int argc, char  **argv) {

    vector<int> ivec;
    ivec.reserve(10);

    ivec[11] = 10;

    Funct();

    cout << "end" << endl;

    return 0;
}

这段程序越界写入数组会导致 crash,但是 core 文件定位的时候并不好找,bt 打印的信息:

(gdb) bt
#0  0x00007fb5e6f581f7 in raise () from /lib64/libc.so.6
#1  0x00007fb5e6f598e8 in abort () from /lib64/libc.so.6
#2  0x00007fb5e6f97f47 in __libc_message () from /lib64/libc.so.6
#3  0x00007fb5e6f9f619 in _int_free () from /lib64/libc.so.6
#4  0x00000000004027c4 in __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, int> > >::deallocate (this=0x7ffe37e10140, __p=0xc52040) at /usr/include/c++/4.8.2/ext/new_allocator.h:110
#5  0x00000000004022ee in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_put_node (
    this=0x7ffe37e10140, __p=0xc52040) at /usr/include/c++/4.8.2/bits/stl_tree.h:374
#6  0x00000000004019a4 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_destroy_node (
    this=0x7ffe37e10140, __p=0xc52040) at /usr/include/c++/4.8.2/bits/stl_tree.h:422
#7  0x00000000004012d5 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_erase (
    this=0x7ffe37e10140, __x=0xc52040) at /usr/include/c++/4.8.2/bits/stl_tree.h:1127
#8  0x0000000000400f46 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::~_Rb_tree (
    this=0x7ffe37e10140, __in_chrg=<optimized out>) at /usr/include/c++/4.8.2/bits/stl_tree.h:671
#9  0x0000000000400ee6 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::~map (this=0x7ffe37e10140, __in_chrg=<optimized out>)
    at /usr/include/c++/4.8.2/bits/stl_map.h:96
#10 0x0000000000400d9e in Funct () at main.cpp:10
#11 0x0000000000400e0a in main (argc=1, argv=0x7ffe37e102c8) at main.cpp:21

从这段栈信息中并不能直观的定位到出现问题的地方,因为出现问题的地方是之前的数组越界,但是这里的栈信息已经执行到 Funct 中的 map, 特别是在大项目中更是无法准确的定位出错的地方。
越界写坏内存,什么时候会 crash 取决于我们写坏的内存什么时候被用到,有时候会离犯罪现场特别远,因为真的有人会写出对一个 10 个元素的数组用 vec[50] = xxx 的代码。

使用 valgrind 进行越界检查。

相关文章

  • GDB 难以定位的 Crash - 越界访问

    示例代码: 这段程序越界写入数组会导致 crash,但是 core 文件定位的时候并不好找,bt 打印的信息: 从...

  • Crash防护

    Container crash(数组越界,插nil等)NSString crash (字符串操作的crash)NS...

  • Doris开发手记3:利用CoreDump文件快速定位Doris

    Apache Doris的BE部分是由C++编写,当出现一些内存越界,非法访问的问题时会导致BE进程的Crash。...

  • Crash in Cocoa

    Crash in Cocoa Cocoa中会导致Crash的地方: Exceptions类型 1. 集合类越界或插...

  • iOS 数组越界 Crash处理

    我们先来看看有可能会出现的数组越界Crash的地方; 上面代码是有可能会越界的;出现Crash也不好复现,发出去的...

  • crash捕获及处理

    一、crash类型 1.OC层面的crash 1.1 普通类型 NSArray 越界 NSCache key或va...

  • 0 <= 0 - 1 == true?

    最近接手一个项目,出现一个crash,通过日志看出是数组越界,并且直接跳到了main函数中,没有定位到错误代码,这...

  • 难以定位的Crash怎么修?

    crash大家肯定都遇到过,也应该遇到过一些没有头绪修不下去的crash, 有些在困扰你很久之后被你搞定,有些只能...

  • iOS Crash解析

    iOS 开发过程中会遇到crash,有些很容易就能定位到,例如数组越界、类型不匹配、方法不存在等。但是有些就比较头...

  • 关于百度地图addAnnotations的bug

    1.bug的出现情况: 程序一跑就crash掉了,log显示为数组越界通过断点定位到bug出现在这一行代码: 这就...

网友评论

      本文标题:GDB 难以定位的 Crash - 越界访问

      本文链接:https://www.haomeiwen.com/subject/decljftx.html