2 ms·
My favorite was C code that was roughly (all functions and variables h: int bar; ... foo() { if(bar) { doX(); else doY(); }
by aidenn0 4y ago
My favorite was C code that was roughly (all functions and variables h:
int bar;
...
foo()
{
if(bar) {
doX();
else
doY();
}
}
Note that uninitialized globals in C are implicitly zero-initialized.
doY() had been broken for years but nobody ever set bar to be true; doX() was nevertheless being called. Setting bar to zero caused the buggy doY() to happen. Setting bar to one seemed to work, but would occasionally segfault.
Then someone explicitly initialized bar and got a linker error. Turns out in ye olden days of C, before "extern" was a thing there was a feature where variables that were initialized in no more than one compilation unit would be all coalesced by the linker. Before doY was broken, someone defined a function named bar, which then caused bar to have a non-zero (i.e. true) value. Someone later added some broken feature to "doY" and it passed all the tests because changing doY() didn't have any effect.
- blep_ 4y agoYou're missing the } and { around your else, btw. I'm only mentioning that because I immediately saw it and got ready to read a horrifying story involving the text "#define else".