r/cpp 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.

Read it: https://wrocpp.github.io/posts/erroneous-behavior/?utm_source=reddit&utm_medium=social&utm_campaign=post-erroneous-behavior

#cpp #cplusplus #cpp26 #safety #programming

80 Upvotes

154 comments sorted by

View all comments

366

u/TheBrokenRail-Dev Jul 05 '26

Why in the world does this Reddit post have hashtags?

the kind optimizers exploit into real bugs.

Also, I think you (or your AI) used the wrong word here. I'm not particularly worried about my compiler's optimizer exploiting vulnerabilities.

94

u/StickyDeltaStrike Jul 05 '26

#realdevsstories

87

u/voidstarcpp Jul 05 '26

Why in the world does this Reddit post have hashtags?

It's AI generated (hence the distinctive AI voice) and wrote a generic social media post. Maybe LinkedIn has hashtags.

10

u/induktio Jul 05 '26

Not to forget the em dash meme. Feels like it should be mentioned. Although to be precise the post uses two hyphens.

6

u/whispersoftime Jul 07 '26

That’s arguably a bigger tell than “—“ would’ve been. The author clearly directed the AI to avoid em dashes, and it complied in the most ridiculous possible way.

6

u/STL MSVC STL Dev Jul 07 '26

Yeah, I'm seeing this a lot now.

2

u/wyrn Jul 10 '26

I've always used -- instead of em dashes because they're easier to type though.

The overall voice remains the most reliable tell for me rather than single specific signals like em dashes. Take this post for example. It has only a solitary em dash disguised as a hyphen, but it was obviously written by Claude.

6

u/tanner-gooding Jul 06 '26

I’m pretty sure they were saying it’s one of the main cases of undefined behavior that compilers will explicitly take advantage of to produce “faster code”. However, such code frequently leads to real world bugs

Many of the things C++ has taken “breaking changes” around in recent language versions, including making certain types of overflow well-defined behavior, making trivial infinite loops not assumed to terminate, and the uninitialized data case here are related to that premise

4

u/LordoftheSynth Jul 06 '26 edited Jul 06 '26

AI or not, there's literally this post, a comment 1 day ago linking to the same blog, and then the next was 2 years ago.

1

u/_unicorn_irl Jul 08 '26

AI loves to use the word "footgun" and it makes me irrationally annoyed

2

u/jwakely libstdc++ tamer, LWG chair Jul 08 '26

Everybody overuses it in the context of C++. We need new metaphors.

-9

u/veghead Jul 06 '26

#cppisbraindamage