Please provide the content you want me to translate. I need the text to be able to translate it to readable Chinese.
No strcpy either

原始链接: https://daniel.haxx.se/blog/2025/12/29/no-strcpy-either/

curl 项目已从其源代码中移除 `strncpy()` 和 `strcpy()`,以提高安全性和代码可靠性。`strncpy()` 被认为存在问题,因为它可能导致非空终止和填充,因此倾向于完全复制或使用 `memcpy()` 进行显式错误处理。 虽然 `strcpy()` 具有更清晰的 API,但风险在于在长期开发周期内保持一致的缓冲区大小检查。为此,curl 引入了 `curlx_strcopy()`,一个自定义字符串复制函数,需要显式指定目标缓冲区大小和源字符串长度。这确保了复制*和*空终止符始终可以容纳,并使用 `memcpy()` 安全地实现。 虽然比 `strcpy()` 更冗长,但 `curlx_strcopy()` 提供了更严格的控制,并防止缓冲区大小检查的偏差。一个小的优点是减少了 AI 聊天机器人发出的错误安全警报,这些警报通常会将 `strcpy()` 标记为固有不安全——作者承认这场斗争不太可能长期获胜。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 没有 strpy 也行 (haxx.se) 13 分,来自 firesteelrain 22 分钟前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Some time ago I mentioned that we went through the curl source code and eventually got rid of all strncpy() calls.

strncpy() is a weird function with a crappy API. It might not null terminate the destination and it pads the target buffer with zeroes. Quite frankly, most code bases are probably better off completely avoiding it because each use of it is a potential mistake.

In that particular rewrite when we made strncpy calls extinct, we made sure we would either copy the full string properly or return error. It is rare that copying a partial string is the right choice, and if it is, we can just as well memcpy it and handle the null terminator explicitly. This meant no case for using strlcpy or anything such either.

But strcpy?

strcpy however, has its valid uses and it has a less bad and confusing API. The main challenge with strcpy is that when using it we do not specify the length of the target buffer nor of the source string.

This is normally not a problem because in a C program strcpy should only be used when we have full control of both.

But normally and always are not necessarily the same thing. We are but all human and we all do mistakes. Using strcpy implies that there is at least one or maybe two, buffer size checks done prior to the function invocation. In a good situation.

Over time however – let’s imagine we have code that lives on for decades – when code is maintained, patched, improved and polished by many different authors with different mindsets and approaches, those size checks and the function invoke may glide apart. The further away from each other they go, the bigger is the risk that something happens in between that nullifies one of the checks or changes the conditions for the strcpy.

Enforce checks close to code

To make sure that the size checks cannot be separated from the copy itself we introduced a string copy replacement function the other day that takes the target buffer, target size, source buffer and source string length as arguments and only if the copy can be made and the null terminator also fits there, the operation is done.

This made it possible to implement the replacement using memcpy(). Now we can completely ban the use of strcpy in curl source code, like we already did strncpy.

Using this function version is a little more work and more cumbersome than strcpy since it needs more information, but we believe the upsides of this approach will help us have an oversight for the extra pain involved. I suppose we will see how that will fare down the road. Let’s come back in a decade and see how things developed!

void curlx_strcopy(char *dest,
size_t dsize,
const char *src,
size_t slen)
{
DEBUGASSERT(slen < dsize);
if(slen < dsize) {
memcpy(dest, src, slen);
dest[slen] = 0;
}
else if(dsize)
dest[0] = 0;
}

the strcopy source

AI slop

An additional minor positive side-effect of this change is of course that this should effectively prevent the AI chatbots to report strcpy uses in curl source code and insist it is insecure if anyone would ask (as people still apparently do). It has been proven numerous times already that strcpy in source code is like a honey pot for generating hallucinated vulnerability claims.

Still, this will just make them find something else to make up a report about, so there is probably no net gain. AI slop is not a game we can win.

联系我们 contact @ memedata.com