Ayende @ Rahien

Refunds available at head office

Spot the bug…

image

Do you see it? It took me ages to find it.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Fredrik
07/02/2013 09:10 AM by
Fredrik

Ha ha, it's quite subtle, but so obvious once you spot it.

Sebastian
07/02/2013 09:11 AM by
Sebastian

I think there is something 'else' missing ;-)

Sam
07/02/2013 09:26 AM by
Sam

LOL, I didn't saw it either

Marc Brooks
07/02/2013 09:46 AM by
Marc Brooks

Not to mention the sheer lunacy of mid not being related to target in any discernable way. Ugh.

(oh, yeah, and missing else)

P.Czatrowski
07/02/2013 10:00 AM by
P.Czatrowski

Apart from missing else, what about arithmetic overflow?

I would change the line: var mid = (left + right + 1) / 2;

to this: var mid = left + (right - left + 1) / 2;

You still get the same result, but it's a little bit safer for higher values.

tobi
07/02/2013 10:08 AM by
tobi

The bug aside, I'd use checked((left + right + 1) / 2).

Roman Boiko
07/03/2013 08:25 AM by
Roman Boiko

Another potential problem: behavior of comparator might be not aligned with subtraction, so that

_comparator.Compare(mid-1, mid) >=0

Alex
07/05/2013 07:48 AM by
Alex

This is really a tricky problem to spot. :) Everyone is focused on the code inside the brackets and the missing "else" is not being observed very fast.

Adam Fridental
07/12/2013 09:18 AM by
Adam Fridental

Sorry to dissapoint, took me a second.

Anyway, I don't belive in scoping braces when they contain 1 line, it's not safer and not easier to read.. just redundant.

Comments have been closed on this topic.