stack smashing detected

#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按回车

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值