这个代码编译能通过,执行为什么报错堆栈cookie检测代码检测到基于堆栈的缓冲区溢出
我的答案
#include
using namespace std;
char*strcpy(char *dest, const char *source);
int main()
{
char a[20];
const char b[20] = "asjdflkjl2s";
cout<<strcpy(a, b)<return 0;
}
char*strcpy(char *dest, const char *source)
{
for (int i = 0; dest[i]!='\0'; i++)
{
dest[i] = source[i];
}
return dest;
}
for循环里面的判断条件错了,dest[i]!='\0'
要改成source[i]!='\0'
,dest里面没有'\0'字符。还有拷贝完后,还要给dest的末尾加上'\0'
int i = 0
while(source[i] != '\0')
{
dest[i] = source[i];
i++;
}
dest[i] = '\0';