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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Undefined Behavior in C and C++

28 点作者 rwallace大约 1 年前

9 条评论

tyfighter大约 1 年前
I keep finding myself angry about the recent (some number of years) focus on C and C++&#x27;s undefined behavior. I have been writing C and C++ for 27 years, 16 years professionally, and despite all the scary implications, I do not understand why <i>ANYONE</i> cares. I do not get it. This is yet another article that goes on and on about nonsensical situations that are just shitty code. Integer overflow? Who cares? Unless you&#x27;re targeting a specific compiler and architecture, it doesn&#x27;t matter. C and C++ have footguns. Everyone knows that. Who cares?<p>I am anger commenting, because I&#x27;m just sick of this, but this article still says nothing to convince me that any of this matters.
评论 #39487983 未加载
评论 #39488664 未加载
评论 #39488307 未加载
评论 #39488262 未加载
评论 #39488357 未加载
olliej大约 1 年前
A core part of the problem of UB in C and C++, is that it is gratuitously over applied.<p>Mercifully the article calls out the BS argument of &quot;old hardware&quot; justifying UB. It is simply a false argument. The overwhelming majority of UB in C and C++ should be either implementation defined or unspecified behaviour. Security vulnerabilities due to overflow or null dereferences being UB should never have been possible because there are no platforms in which those operations are not defined (some trap, some wrap, some go to infinity), but that is all under the banner of implementation defined behavior. Labelling these things as UB is _solely_ to allow performance optimizations in narrow cases, at the cost of safety in all cases.<p>In committee meetings I&#x27;ve been in recently the new refrain I&#x27;m hearing&#x2F;reading that has replaced &quot;we need to support various hardware&quot; is an even more stupid argument: if we make it so that these aren&#x27;t UB then people will rely on the common behavior and write code that is incorrect on platforms that behave differently. e.g. instead of software that is always wrong on one platform, you make software that is semi-randomly wrong on all platforms (because whether or not a compiler removes UB in one case is dependent on compiler version, flags, inlining, etc and if any of those change then suddenly the same code you had yesterday has a security bug when shipped).
评论 #39488631 未加载
nullhole大约 1 年前
My favourite description of undefined behaviour. The poster is corrected later on in the thread about whether the specific operation discussed would invoke undefined behaviour, but the description of what happens when undefined behaviour occurs is gold:<p><a href="https:&#x2F;&#x2F;groups.google.com&#x2F;g&#x2F;comp.lang.c&#x2F;c&#x2F;ZE2B2UorTtM&#x2F;m&#x2F;1ROv8gTwuEAJ" rel="nofollow">https:&#x2F;&#x2F;groups.google.com&#x2F;g&#x2F;comp.lang.c&#x2F;c&#x2F;ZE2B2UorTtM&#x2F;m&#x2F;1ROv...</a><p>Joona I Palaste, 2001-01-19, comp.lang.c<p><pre><code> This isn&#x27;t about the post-increment operator, this is about the order of evaluation of the operands. Since you&#x27;re modifying the value of i twice without a sequence point in between, either of the two results are exactly as much &quot;expected&quot;. Also, equally &quot;expected&quot; behaviour includes incrementing every variable in the array, flipping all the bits in every variable in the array, converting all instances of the text string &quot;&#x2F;usr&quot; in memory to &quot;fsck&quot;, changing the colours of your screen to purple, calling the police on your modem line and telling them you&#x27;re being attacked by a one-eyed Martian wielding a herring while singing &quot;Hi ho, it&#x27;s off to work we go&quot;, and even weirder stuff. So... what it all boils to... when writing your compiler, just flip a coin and use the one of the two behaviours you listed that corresponds with the coin&#x27;s face.</code></pre>
评论 #39488598 未加载
rwallace大约 1 年前
Not the first discussion of this topic, by any means. In this case, I&#x27;ve tried to boil it down to the essential points a practical programmer needs to know, but the article still ended up longer than I initially aimed for.
评论 #39488090 未加载
Jun8大约 1 年前
Here&#x27;s another interesting post if you want to delve further into an example of undefined behavior created by gcc optimization: <a href="https:&#x2F;&#x2F;thephd.dev&#x2F;c-undefined-behavior-and-the-sledgehammer-guideline" rel="nofollow">https:&#x2F;&#x2F;thephd.dev&#x2F;c-undefined-behavior-and-the-sledgehammer...</a>.<p>Also, this quote comes to mind: &quot;C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off&quot;: <a href="https:&#x2F;&#x2F;www.stroustrup.com&#x2F;quotes.html" rel="nofollow">https:&#x2F;&#x2F;www.stroustrup.com&#x2F;quotes.html</a>
评论 #39488695 未加载
评论 #39488361 未加载
andy99大约 1 年前
In the bit where he shows<p><pre><code> void error(const char* msg); int successor(int a) { if (a + 1 &lt; a) error(&quot;Integer overflow!&quot;); return a + 1; } </code></pre> and says the if is compiled away at -O3, does any one know if it remains at any lower optimization level? I know some of the more aggressive optimizations intentionally ignore some checks, I don&#x27;t know if that applies here. I found the -O3 odd for trying to help make his point, unless it doesn&#x27;t work at -O2.
评论 #39488502 未加载
layer8大约 1 年前
I recommend reading the resources under <a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;c&#x2F;language&#x2F;behavior#External_links" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;c&#x2F;language&#x2F;behavior#External_l...</a> (–&gt; External links).
iwsk大约 1 年前
I don&#x27;t get it.<p>How can UB on double-free, use-after-free, dangling pointers, etc lead to optimizations?
评论 #39488835 未加载
mjevans大约 1 年前
Once again, I want to plead. At least have a Warning option to annotate any time undefined behavior is encountered by a compiler. The goal should be to promote optimizations to written code and improve code quality. Not just the result of one particular compiler.
评论 #39488365 未加载
评论 #39488409 未加载
评论 #39488296 未加载