The Daily WTF
Oliver Smith stumbled across a small but surprising bug when running some automated tests against a mostly clean code-base. Specifically, they were trying to break things by playing around with different compiler flags and settings. And they did, though in a surprising case.
bool long_name_that_maybe_distracted_someone() { return (execute() ? CONDITION_SUCCESS : CONDITION_FAILURE); }
Note the return type of the method is boolean. Note that execute must also return boolean. So once again, we’ve got a ternary which exists to turn a boolean value into a boolean value, which we’ve seen so often it’s barely worth mentioning. But there’s an extra, hidden assumption in this code- specifically that CONDITION_SUCCESS and CONDITION_FAILURE are actually defined to be true or false.
CONDITION_SUCCESS was in fact #defined to be 1. CONDITION_FAILURE, on the other hand, was #defined to be 2.
Worse, while long_name_that_maybe_distracted_someone is itself a wrapper around execute, it was in
To read the full article click on the 'post' link at the top.