Article 6DWQY CodeSOD: Random Comparison

CodeSOD: Random Comparison

by
Remy Porter
from The Daily WTF on (#6DWQY)

Justin's co-worker needed to validate a UUID/GUID in C#. So they wrote this:

if (objectId == null || objectId.ToString() == (new Guid()).ToString()){ result = new Error(Error.ERR_REQUEST_INVALID);}

The first thing to note here is that Guid objects in C# are non-nullable value types. That is to say, objectId cannot possibly be null. Oddly, you can still compare it against null, which is fine, but meaningless.

But that's not the worst thing here, as the second clause in that condition is... something.

If objectId happens to be the same as a new GUID converted to a string, this is an invalid request. Which, I suppose that's true- the odds of the input GUID matching a randomly generated GUID are so low, we'd need to assume that we're living in the Matrix, or that the heat death of the universe is rapidly approaching or, probably more likely, your random number generator is really bad.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
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