Article 3Q5C1 CodeSOD: CONDITION_FAILURE

CodeSOD: CONDITION_FAILURE

by
Remy Porter
from The Daily WTF on (#3Q5C1)

Oliver Smith sends this representative line:

bool long_name_that_maybe_distracted_someone(){ return (execute() ? CONDITION_SUCCESS : CONDITION_FAILURE);}

Now, we've established my feelings on the if (condition) { return true; } else { return false; } pattern. This is just an iteration on that theme, using a ternary, right?

That's certainly what it looks like. But Oliver was tracking down an unusual corner-case bug and things just weren't working correctly. As it turns out, CONDITION_SUCCESS and CONDITION_FAILURE were both defined in the StatusCodes enum.

badfeeling.png

Yep- CONDITION_FAILURE is defined as 2. The method returns a bool. Guess what happens when you coerce a non-zero integer into a boolean in C++? It turns into true. This method only ever returns true. Ironically, the calling method would then do its own check against the return value, looking to see if it were CONDITION_SUCCESS or CONDITION_FAILURE.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today! TheDailyWtf?d=yIl2AUoC8zA7Hsl0fJL1ig
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments