问题描述
char string[5] = "stringBiggerThan5"; // shows error. Okay.
strcpy(string,"big string"); // overflow. seg fault. Perfect!
但是,向大于数组大小的 char[]
添加字符串不会显示任何错误:
char string[5];
strcat(string,"stringBiggerThan5"); // no error. Why??!
再说一次,没有错误:
char* simple_string_func() {
static char string[5];
strcpy(string,"hugeBigString");
strcat(string,"tryingToOverflowYou");
return string; //returns all perfectly,without errors
}
请注意,将 strcpy
放大到 static char[5]
现在不会显示 overflow error
或 segementation fault
!为什么?
如果我在函数中为 return
分配字符串的方法理想/没问题,请公开发表评论。
解决方法
区别在于您看到的编译时检查和 C 没有的运行时检查。
我建议您再考虑一下您要做什么,重点关注保存字符串所需的内存以及它的来源。如果不涉及线程,事情会更简单,但例如,如果您的例程被调用两次,则当前会出现问题。
,当您尝试放置比给定大小更大的字符数组时,仍然会发生缓冲区溢出并调用 undefined-behavior。使用 -O2 编译器标志进行编译时会出现错误(最后两行清楚地总结了所有内容):
In file included from /usr/include/string.h:495,from main.c:2:
In function ‘strcat’,inlined from ‘simple_string_func’ at main.c:6:3:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:128:10: warning: ‘__builtin___strcat_chk’ writing 14 bytes into a region of size 5 overflows the destination [-Wstringop-overflow=]
128 | return __builtin___strcat_chk (__dest,__src,__bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘strcat’,inlined from ‘simple_string_func’ at main.c:6:3,inlined from ‘main’ at main.c:12:24:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:128:10: warning: ‘__builtin___strcat_chk’ writing 14 bytes into a region of size 5 overflows the destination [-Wstringop-overflow=]
128 | return __builtin___strcat_chk (__dest,__bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** buffer overflow detected ***: terminated
Aborted (core dumped)
strcat(3) 文档说:
strcat()
函数将 src 字符串附加到 dest
字符串,
覆盖 dest
末尾的终止空字节 ('\0'),并且
然后添加一个终止空字节。字符串不能重叠,并且
dest
字符串必须有足够的空间用于结果。如果 dest
是
不够大,程序行为不可预测。
即使使用 static char[]
live here,您也可以清楚地看到优化级别 2 的溢出。
请注意,所附的编译输出是由 gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 生成的9.3.0