Ticket #68 (closed defect: fixed)

Opened 1 year ago

Last modified 2 months ago

Bug in Binary Search

Reported by: newmana Assigned to: pag
Priority: minor Milestone:
Component: Mulgara Version: 1.0.0
Keywords: Cc:

Description

TripleAVLFile has the same bug as discussed by Joshua Bloch (http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html). The middle calculation can overflow into negative if left + right is greater than MAX_INT. Probably not a big problem yet as all calls are 0, nrTriples where nrTriples is the block size which is less than MAX_INT.

Currently:
    for (;;) {
      // if the range is zero then the node was not found.
      // return the next node up.
      if (left >= right) {
        return -right - 1;
      }

      // find the middle of this range
      int middle = (left + right) / 2;
      // determine if the required triple is above or below the middle
      int c = comp.compare(triple, triples, middle);

      // if it's in the middle then return it
      if (c == 0) {
        return middle;
      }

      if (c < 0) {
        // if it's below the middle then search there
        right = middle;
      } else {
        // if it's below the middle then search there
        left = middle + 1;
      }

Change:
      int middle = (left + right) / 2;
To:
      int middle = (left + right) >>> 1;

Also, the developers page (http://mulgara.org/developers.html) points "please report it." to ihttp://... instead of http://.

Change History

10/15/08 00:01:57 changed by pag

  • status changed from new to closed.
  • resolution set to fixed.

(In [1318]) Using an arithmetic shift for halving. Not a big deal, but it's more correct, and it now fixes #68