Bug in Binary Search after twenty years

Bug in Binary Search after twenty years

Binary Search which was written in 1986,detected a bug and it was reported to Sun recently when it broke someone's program, after lying in wait for nine years or so.
 
The original code go this way,

  1. public static int binarySearch(int[] a, int key) {
  2. int low = 0;
  3. int high = a.length - 1;
  4. while (low <= high) {
  5. int mid = (low + high) / 2;
  6. int midVal = a[mid];
  7. if (midVal < key)
  8. low = mid + 1;
  9. else if (midVal >; key)
  10. high = mid - 1;
  11. return mid; // key found
  12. }
  13. return -(low + 1);  // key not found.
  14. }

 

The bug is in this line:

5: int mid =(low + high) / 2;

The above line of code suggest that it sets mid to average value of low and high,trucated down to the nearest integer.It might appear correct,but it fails for large values of the int variables low and high.Specifically, it fails if the sum of low and high is greater than the maximum positive int value (2^31 - 1).The sum overflows to a negative value, and the value stays negative when divided by two.

Errors:
In "C" this causes an array index out of bounds with unpredictable results.
In "Java",it throws ArrayIndexOutOfBoundsExce ption.

So what's the best way to fix the bug? Here's one way:

5: int mid = low + ((high - low) / 2);

Probably faster, and arguably as clear is:

5: int mid = (low + high) >>> 1;

In C and C++ (where you don't have the >>> operator), you can do this:

5: mid = ((unsigned) (low + high)) >> 1;


The binary-search bug applies equally to mergesort, and to other divide-and-conquer algorithms. If you have any code that implements one of these algorithms, fix it now before it blows up. The general lesson that I take away from this bug is humility: It is hard to write even the smallest piece of code correctly, and our whole world runs on big, complex pieces of code.



posted by: surrogate (reply)
post date: 07.04.06 (5:07 am)

okay, okay. I feel even dumber now. (I just BARELY understand the rudiments of how binary code works, and I mean, okay... got it... there are ones and zeros and everything's based on that.... Beyond that? No clue.)



posted by: seochris (reply)
post date: 07.04.06 (6:59 pm)

It was a bad error from sun's part. Better someone dicovered it may be there are more such bugs else where. We should be alert to the codes.

Your Name:


Your Comment: