TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

C considered dangerous

45 点作者 johnramsden超过 6 年前

8 条评论

rwmj超过 6 年前
<i>&gt; He asked: why is there no argument to memcpy() to specify the maximum destination length?</i><p>I&#x27;m confused by this. The third argument provides the destination length, so what good would a &quot;maximum destination length&quot; do? I guess he must mean that because the length is often computed, you&#x27;d need a fourth argument to ensure the length isn&#x27;t greater than some sane upper bound. But you can easily fix that using an if statement around the memcpy.
评论 #17874742 未加载
评论 #17892394 未加载
deng超过 6 年前
Thankfully, compiler warnings and static analyzers have become much better in recent years. For instance, gcc can now warn about a missing &#x27;break;&#x27; mentioned in the article (you need to add a special comment like &#x27;&#x2F;* fall through *&#x2F;&#x27; if it&#x27;s intentional). Also, clang-tidy is getting better with each release. I highly recommend using it, although the initial configuration will take some time, depending on the code base.
xroche超过 6 年前
Alas! strlcpy and strlcat are still not present in the glibc, despite numerous attempts, mainly for religious reasons (ie. &quot;BSD sucks&quot;).<p>And yes, having something like &quot;if (strlcat(buffer, src, sizeof(buffer) &gt;= sizeof(buffer)) { abort(); } &quot; is much better than buffer overrun. But security does not always seem to be a real concern, compared to politics.
yason超过 6 年前
C is dangerous partly because assembly language is dangerous. We will always need some layer on top of assembly that is mostly unchecked and reflects back to how cpu instructions work. This is probably something we must live with until we have processors with the notion of type checking.<p>C is dangerous partly because of swaths of undefined behaviour and loose typing. Eliminating much of undefined behaviour either by defining the behaviour or forcing the compiler to refuse compile undefined behaviour could be of some help. There are still classes of undefined behaviour that cannot be worked around but narrowing that down to a minimal set would make it easier to deal with it. Strong typing would help build programs that won&#x27;t compile unless they are correct at least in terms of types of values.<p>C is dangerous partly because of the stupid standard library which isn&#x27;t necessarily a core language problem as other libraries can be used. The standard library should be replaced with any of the sane libraries that different projects have written for themselves to avoid using libc. It&#x27;s perfectly possible not to have memcpy() or strcpy() like minefields or strtok() or strtol() which introduce the nice invisible access to internal static storage, fixed by a re-entrant variant like strtok_r(), or require you to do multiple checks to determine how the function actually failed. The problem here is that if there are X standards, adding one to replace them all will make it X+1 standards.<p>Yet, good programmers already avoid 99% of the problems by manually policing themselves. For them, C is simple, productive, and manageable in a lot more cases and domains than it is for the less experienced programmers.
评论 #17875766 未加载
IshKebab超过 6 年前
Terrible title. It&#x27;s not remotely news that C is dangerous. This talk seems to be about ways of mitigating the dangers. Why not call it &quot;Mitigating the dangers of C&quot; or something else that is less of a tired cliche?
评论 #17874982 未加载
fithisux超过 6 年前
The title is completely misleading.
评论 #17874709 未加载
评论 #17874695 未加载
评论 #17874654 未加载
xvilka超过 6 年前
Hopefully Zig [1] language will become a better alternative to C in upcoming years. Not talking about higher level code where Rust or Go can be a better choice.<p>[1] <a href="https:&#x2F;&#x2F;ziglang.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ziglang.org&#x2F;</a>
评论 #17875871 未加载
评论 #17875287 未加载
amelius超过 6 年前
Why don&#x27;t they use valgrind?
评论 #17875069 未加载