内存越界的一种定位方法
来源:互联网 发布:斑马斑马 知乎 编辑:程序博客网 时间:2024/05/16 09:33
#include <stdlib.h>#include <stdio.h>#include <string.h>void fn(char *str){ memset(str, 0, 64); return;}int main(int argc, char **argv){ char badstr[32] = "abc"; int fd = 1; printf("badstr = %s\n", badstr); printf("fd = %d\n", fd); fd = 2; printf("fd = %d\n", fd); fn(badstr); printf("fd = %d\n", fd); printf("badstr = %s\n", badstr); return 0;}
上述代码明显内存越界,一个watch搞定。
以下是定位过程:
[root@localhost qiyk]# ./test
badstr = abc
fd = 1
fd = 2
fd = 0
badstr =
总线错误[程序因内存越界异常退出]
[root@localhost qiyk]# ./gdb test
GNU gdb Red Hat Linux (6.6-8.fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License,
welcome to change it and/or distribute copies of it under certain
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" fo
This GDB was configured as "i386-redhat-linux-gnu"...
Using host libthread_db library "/lib/i686/nosegneg/libthread_db.
(gdb) b main
Breakpoint 1 at 0x80484cb: file test.cpp, line 13.
(gdb) r
Starting program: /home/qiyk/test
Breakpoint 1, main () at test.cpp:13
13 char badstr[32] = "abc";
(gdb) n
14 int fd = 1;
(gdb) watch fd
Hardware watchpoint 2: fd
(gdb) c
Continuing.
Hardware watchpoint 2: fd[第一次人为修改,此处中断]
Old value = 6317008
New value = 1
main () at test.cpp:15
15 printf("badstr = %s\n", badstr);
(gdb) c
Continuing.
badstr = abc
fd = 1
Hardware watchpoint 2: fd[第二次人为修改,此处中断]
Old value = 1
New value = 2
main () at test.cpp:18
18 printf("fd = %d\n", fd);
(gdb) c
Continuing.
fd = 2
Hardware watchpoint 2: fd[第三次意外修改,此处中断]
Old value = 2
New value = 0
0x004ea367 in memset () from /lib/i686/nosegneg/libc.so.6
(gdb) bt[查看现场堆栈]
#0 0x004ea367 in memset () from /lib/i686/nosegneg/libc.so.6
#1 0x080484b8 in fn (str=0xbf92bd20 "") at test.cpp:7
#2 0x0804854d in main () at test.cpp:19
(gdb) up
#1 0x080484b8 in fn (str=0xbf92bd20 "") at test.cpp:7
7 memset(str, 0, 64);[问题点出现:str越界,导致fd值变为0]
(gdb) q
The program is running. Exit anyway? (y or n) y
- 内存越界的一种定位方法
- 一种定位内存泄露的方法(Solaris)
- 一种定位内存泄露的方法(Linux)
- 一种定位内存泄露的方法(Solaris)
- 一种定位内存泄露的方法(Linux)
- 一种定位内存泄露的方法(Linux)
- 一种定位内存泄露的方法(Linux)
- 一种定位内存泄露的方法(Solaris)
- 一种定位内存泄露的方法(Linux)
- 一种定位内存泄露的方法(Linux)
- 又一种内存越界的情况, 野指针导致sprintf函数内存越界
- 内存越界的现象及调试方法
- 内存越界的概念和调试方法
- 内存越界的概念和调试方法
- 内存越界的概念和调试方法
- 内存越界排查方法
- 定位多线程内存越界问题实践总结
- 定位多线程内存越界问题实践总结
- Android 软件开发之数据的 新建 储存 读取 删除 详解
- USACO/fence 3.3.1 欧拉回路
- android bitmap
- 子jsp给父jsp传参
- Hiero工具的利用
- 内存越界的一种定位方法
- C#中Application类简述
- MySQL 1364 错误提示:#1364 - Field "XX" doesn't have a def...
- Nginx rewrite 配置问号解决办法
- 去掉NSString空字符串
- oracle 的pdm和dmp含义
- 带文字的ImageButton
- 创业公司老板管理常识
- MC新手入门(四十七)------ 重力感应