- 主题:strncpy的size指定超过dest的长度会有问题吗?
char *strncpy(char *dest, const char *src, size_t n);
项目中遇到一个问题,就是代码bug导致n设定的比dest实际长度大了一些
然后发生了越界写入,把其他的数据给写坏了
修改了这一点之后问题解决
不过不确定这两者是否真的有关,因为调查了一下src实际的长度远是小于dest的,并不会越界
想问问在主流的strncpy实现里面,有可能因为指定了过大的size(但是dest还是比src的长度大),导致什么问题吗?从我了解的比较简单的strncpy实现逻辑来看,不应该有这种问题才对
--
修改:timeleap FROM 221.223.26.*
FROM 221.223.26.*
是,100%确定
【 在 haha103 的大作中提到: 】
: 你的src是否100%确定是null-terminated?
: 不过strncpy非常坑跌,建议能不用就不用
--
FROM 221.223.26.*
谢谢,懂了
【 在 nwn 的大作中提到: 】
: 读读文档就知道了
: The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character isn't appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count.
: strncpy 不是最多复制 count 个字符,而是 *一定* 复制 count 个字符。
: ...................
--
FROM 221.223.26.*