CodeSOD: Unequal Code

This post was originally published on this site

The Daily WTF

Ryan‘s co-worker caught a high priority ticket- certain features of their dashboard app were crashing when anyone tried to access them. It didn’t take long to figure out that there was a stack overflow, and that some recursive function was blowing out the stack.

It took a little longer to find the recursive function in their C# code base:

public static bool operator !=(MatrixObjectKey a, object b) { if (a != null) return (a.Equals(b) == false); else return false; }

This is an overload for the != operator, which compares a MatrixObjectKey against any arbitrary object. “Any arbitrary object” would include nulls, so a != null invokes this function.

This, by the way, is why operator overloading is often considered a code smell- you can be surprised by the behavior of seemingly innocuous code. But the bigger smell is how this code got released– somehow this never got caught

To read the full article click on the 'post' link at the top.