#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *test_str = "1232&abdceffdafa&675809";
int get_new_str(const char *srcstr,char *newstr)
{
char *beginstr = strstr(srcstr,"&");
char *endstr = strstr(beginstr+1,"&");
if(beginstr != NULL && endstr != NULL)
{
memcpy(newstr,beginstr+1,endstr-beginstr);//此处报栈内存溢出
}
}
int main()
{
char newstr[5]={0};
get_new_str(test_str,newstr);
printf("newstr:%s\n",newstr);
return 0;
}
gcc test11.c -o test11 -g
运行以上程序报错:
newstr:abdceffdafa&켺70@
*** stack smashing detected ***: ./test11 terminated
Aborted (core dumped)
coredump文件生成一般与运行程序同目录,文件名以core开头,
有时core文件却没有生成,此时设置一下环境变量
ulimit -c unlimited
gdb调试core文件
gdb -c core [应用程序的路径]
进去后输入where回车, 可以看到在哪个函数中.dump掉,但是好像看不到行号
Core was generated by `./test11'.
Program terminated with signal SIGABRT, Aborted.
#0 0x00007fadde97a438 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) where
#0 0x00007fadde97a438 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007fadde97c03a in __GI_abort () at abort.c:89
#2 0x00007fadde9bc7fa in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7faddead459f "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007faddea5e25c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x7faddead4581 "stack smashing detected") at fortify_fail.c:37
#4 0x00007faddea5e200 in __stack_chk_fail () at stack_chk_fail.c:28
#5 0x00000000004006be in main () at test11.c:22
退出gdb
输入q按回车