The production bug that made me care about undefined behavior

Payments app says yes and no at once — Reddit screams 'haunted code' while vets say just initialize

TLDR: A payments API returned both success and error due to reading unset data in C++. Commenters split: some blame the language’s “undefined behavior,” others say just initialize fields and use debugging tools. It matters because real money systems can get weird without safe defaults and checks.

A money-processing API replied “error: true” AND “succeeded: true,” and the internet did what it does best: turned a boring bug into a soap opera. Philippe Gaultier’s post lays out how reading data before it’s set—aka undefined behavior (when the language doesn’t say what happens)—turned a simple response into Schrödinger’s checkout. The r/programming crowd lit up like a Christmas tree.

Strongest takes? A camp led by titzer shouts “this is the language’s fault” and wants default initialization (fields start at safe values). The practical crowd, like vhantz, rolls its eyes: “Just set them to false, don’t trust the compiler.” Then nneonneo drops a cold shower: calling uninitialized values “garbage” is too cute—compilers can assume whatever helps them optimize, so reality gets weird fast. Old-school war stories arrive via mac3n, who recalls Fortran literally not allocating memory and variables overlapping like roommates. Jokes flew: “Schrödinger’s JSON,” “ghosts in the memory,” and “Boolean bigamist.” Tool lovers argued that sanitizers (debug helpers that catch bad reads) would have saved the day, while others say the language should stop handing out footguns. Kayo_20211030 summed the mood: funny now, terrifying then.

Key Points

  • A production HTTP payments endpoint returned both error=true and succeeded=true, which should be mutually exclusive.
  • The function constructed a Response struct without explicit initialization, setting succeeded in try and error in catch.
  • Default-initialization in C++ left built-in bool members uninitialized, and reading them caused undefined behavior.
  • Undefined behavior explained the contradictory response despite each flag being assigned in only one code path.
  • The author reviewed the C++ standard, provided a minimal example via Compiler Explorer, and outlined follow-up safeguards.

Hottest takes

"IMHO this is the language's fault" — titzer
"Nothing is gained by leaving it to the compiler, and a lot is lost" — vhantz
"Even calling uninitialized data 'garbage' is misleading" — nneonneo
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.