r/cpp • u/filipsajdak • Jul 05 '26
C++26 ends a 40-year footgun
Reading an uninitialized variable has been undefined behavior in C++ for 40 years -- the kind optimizers exploit into real bugs. C++26 (P2795) reclassifies it as erroneous behavior: still a bug, still warned about, but defined, bounded, and not exploitable.
The demo poisons the stack, then reads an uninitialized int. As C++23 it prints garbage; as C++26, the same code prints a defined 0, every run. Live in your browser.
And [[indeterminate]] lets you opt back out when you really want an uninitialized buffer -- on purpose this time.
#cpp #cplusplus #cpp26 #safety #programming
•
u/STL MSVC STL Dev Jul 05 '26
Moderator warning: AI-generated posts are not allowed on this subreddit. I'll leave this up due to the discussion it's generated.
366
u/TheBrokenRail-Dev Jul 05 '26
Why in the world does this Reddit post have hashtags?
Also, I think you (or your AI) used the wrong word here. I'm not particularly worried about my compiler's optimizer exploiting vulnerabilities.
204
u/bitsynthesis Jul 05 '26
couldn't even write a simple post like this without AI huh? sad
76
u/MrUnknownFrog Jul 05 '26
#toolazytowritetwoparagraphs #withoutAI
55
u/Charming-Work-2384 Jul 05 '26
I t was long over due... finally ...
better late than never
14
u/The3ncy Jul 05 '26
the f is that post text. I mean great feature and all, but c'mon
26
u/Chuu Jul 05 '26
Is there a tl;dr on how this might interact with techniques that are technically UB but widely supported like type punning structs over buffers to read them?
9
u/johannes1971 Jul 06 '26
Rather than complain about the AI post, can we maybe have some discussion about this statement?
Does it? I mean, I'm totally onboard with this, but my understanding was that the value was not specified, so there is no guarantee that it is zero.
12
u/Ami00 Jul 05 '26
any extra cost for that?
4
u/victotronics Jul 05 '26
How does this relate to automatic arrays?
4
u/Som1Lse Jul 06 '26
In response to this post. (He blocked me, so I can't respond directly, but it is an interesting question that deserves an answer.)
To quote:
The answer is yes in GCC, and no in general:
The standard only requires that the value that is consistent and independent of the program state, i.e., it may not just use what was already there, but a compiler can pick any value it wants. The compiler is also allowed to stop the program then and there* in which case the value doesn't matter.
In practice GCC has chosen to just use
-ftrivial-auto-var-init=zero, unless overwritten. Here's the relevant commit and here it is on the GitHub mirror. From the commit message:Personally, I think this is a good default, since you won't see differences between debug and release builds unless you explicitly specify
-ftrivial-auto-var-init.* Pedant note: I believe the compiler is also allowed to stop the program at a later point, due to practical reasons. In that case the compiler still has to pick a consistent value. The compiler is also allowed to issue a diagnostic at runtime, but then keep running the program. Again, it has to pick a consistent value for that.
15
u/specialpatrol Jul 05 '26
So surely old code bases littered with undefined variantse are suddenly going to start performing far worse as everything suddenly gets default initialised?
8
u/New_Age_Dryer Jul 06 '26
Reddit has become such shit
10
u/Arthemio2 Jul 05 '26
Just initialize your variables
20
u/TheRealSmolt Jul 05 '26
Call me old fashioned but I don't really care for this change.
3
u/IceMichaelStorm Jul 05 '26
why do we need the annotation to bring back in. Does it ever effectively provide performance?
Hm, maybe uninitialized arrays in a hot loop?
4
u/madbuilder Jul 06 '26
Why couldn't this be an error? Why force everyone to pay the penalty of zero-filling their stack vars?
3
u/caroIine Jul 05 '26
Will there be nonstandard compiler flag to disable it?
3
u/Benilda-Key Jul 05 '26
I want C++ to have more foot guns, not fewer foot guns!
2
u/Depixelate_me Jul 07 '26
Why must you use AI to write EVERY WORD?? At least remove hashtags etc.... why be so lazy? Invest a few more minutes before posting. At the very least head post by acknowledging the use of AI in writing this article.
2
u/t_hunger Jul 05 '26
Its not always 0, is it?
Why don't you just error out when an uninitialized read happens? That's supposedly one of the simplest things profiles could do, so why not standardise on that behavior?
Will this trigger instances of the debiqn ssh keygen issue they had a few years back? They initialized a value and severly reduced the keys that can get generated.
1
u/fdwr fdwr@github 🔍 Jul 05 '26
Does this apply to
newallocations too, or just local variables?1
u/ezoe Jul 05 '26
That's how it should be.
I really hate compiler developer on this behaviour. Yes, undefined behaviour shall not happen. But assuming that and eliminate code silently often lead to puzzling behaviour.
When code said read a variable explicitly, compilers should emit the code reading a variable.
1
u/biskitpagla Jul 06 '26
Let AI help you write the prompt next time.
1
u/markovchainy Jul 06 '26
So sick of this easily detectable AI voice, it's everywhere. I can't engage with the content because it grates on me
-5
u/_w62_ Jul 06 '26
Without footguns, it is not C++ any more.
-16
u/Sniffy4 Jul 05 '26
footgun is a word ive only seen AI use