It's been quite a while since the last Bring Your Own Code. It's mostly because I haven't thought of any coding quandaries that fit in the "totally fun and doable over a quick break" difficulty; everything has been either hello world easy or graduate-level comp sci homework hard. If you've got any ideas, please do send them to me.

That said, today's BYOC is a little bit different than the previous ones. It was inspired by a submission from Mårten Rånge, who wrote "rumor has it that a disgruntled employee once left #define true false in a random header file in the codebase. Given our codebase, that would be a lot harder to debug than one might think."

"But it got me thinking," Mårten continued, "what is the worst thing a disgruntled employee could leave behind in the source code? This is what I came up with."

#include <cstdlib> #ifndef _DEBUG # undef NULL # define NULL (TheBomb ()) # define CRASH_FREQUENCY 100000 struct TheBomb { template<typename TValue> operator TValue* () const throw () { return (rand() % CRASH_FREQUENCY) ? 0 : (TValue*)((0xFF000000 & (int)this) | (rand () & 0x00FFFFF8)) ; } }; template<typename TValue> bool operator== (TheBomb theBomb, TValue* value) { // Just for fun NULL == will still work properly return !value; } template<typename TValue> bool operator== (TValue* value, TheBomb theBomb) { return (rand() % CRASH_FREQUENCY) ? !value : !!value ; } template<typename TValue> bool operator!= (TheBomb theBomb, TValue* value) { // Just for fun NULL != will still work properly return !!value; } template<typename TValue> bool operator!= (TValue* value, TheBomb theBomb) { return (rand() % CRASH_FREQUENCY) ? !!value : !value ; } #endif

It's pretty clever; basically, the NULL macro randomly does the inverse.

Bring Your Own Code

Your exercise for the day: design a disgruntled bomb. Remember that obfuscation is not the goal; it's more about subtly changing the behavior of something or doing something that's difficult to detect and produces unexpected results. Perhaps it just involves adding one line to an existing codebase, or introducing an entirely re-written library. Be creative. It could be any language on any platform, but just make sure to explain what your "solution" does if it's not completely obvious.

And for the C# folks, don't forget that you can override the true and false operators!