[英]Understanding malloc with char pointers and strcpy
我想编写一个简单的程序,我希望它无法理解 strcpy 和正确的内存管理,但它仍然可以执行。 我正在尝试为一个字符串动态分配内存(使用 malloc),仅足以将 3 个(或任何少于源的)字符用作目标,并分配比源的字符串数组更少的内存(或字符)在堆栈上分配(10 个字符的字符串)。 无论我如何定义内存分配,它都会复制和打印内容。 这里有什么错误?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *ch = NULL;
char name[10] = "something";
ch = (char*)malloc(3 * sizeof(char));
strcpy(ch, name);
printf("%s\n", name);
free(ch);
ch = NULL;
}
越界写入数组的行为是未定义的。 在我的电脑上,没有优化:
% gcc overflow.c
% ./a.out
something
并启用优化
% gcc -O3 overflow.c
In file included from /usr/include/string.h:495,
from overflow.c:2:
In function ‘strcpy’,
inlined from ‘main’ at overflow.c:10:5:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:90:10: warning:
‘__builtin___memcpy_chk’ writing 10 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
90 | return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% ./a.out
*** buffer overflow detected ***: terminated
[2] 240741 abort (core dumped) ./a.out
原因是通过优化 GCC 将实际传播数组大小,并将产生等效于的机器代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char name[10] = "something";
char *ch = malloc(3);
__memcpy_chk(ch, name, sizeof name, 3);
puts(name);
free(ch);
}
__memcpy_chk
会检查目标缓冲区的长度,因为超过了,程序会在运行时中止。
始终在开发时,请记住也要测试您的代码优化是否启用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.