Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-3483

Off-by-one error in FullFastIterator.next() leads to botched queries

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 5.4.0.Final
    • 5.4.0.CR1
    • drools-core
    • None

    Description

      I'll reproduce the entirety of the method() below for clarity:

              public Entry next(Entry object) {
                  RightTuple rightTuple = ( RightTuple ) object;
                  RightTupleList list = null;
                  if ( rightTuple != null ) {
                      list = rightTuple.getMemory(); // assumes you do not pass in a null RightTuple
                  }
      
                  int length = table.length;
      
                  while ( this.row < length ) {
                      // check if there is a current bucket
                      while ( list == null ) {
                          // iterate while there is no current bucket, trying each array position
                          list = (RightTupleList) this.table[this.row];
                          this.row++;
                          
                          if ( list != null ) {
                              // we have a bucket so assign the frist LeftTuple and return
                              rightTuple = (RightTuple) list.getFirst( );
                              return rightTuple;
                          } else if ( this.row >= length ) {
                              // we've scanned the whole table and nothing is left, so return null
                              return null;
                          }
                          
                      }
      
                      rightTuple = (RightTuple) rightTuple.getNext();
                      if ( rightTuple != null ) {
                          // we have a next tuple so return
                          return rightTuple;
                      } else {
                          list = (RightTupleList) list.getNext();
                          // try the next bucket if we have a shared array position
                          if ( list != null ) {
                              // if we have another bucket, assign the first RightTuple and return
                              rightTuple = (RightTuple) list.getFirst( );
                              return rightTuple;
                          }
                      }
                  }
                  return null;
              }
      

      The problem is when we have a RightTupleList in the last row of the table, and that list has more than one RightTuple. The call to next() that brings us to this list will increment this.row so that it's now equal to table.length. A subsequent next() should retrieve the next RightTuple, but because all of that code is conditioned on (table.row < length), the iteration ends prematurely. This affects one of my queries, which for an object with a particular hash code was only returning the first match instead of all of the matches.

      I took a quick look at LeftTupleIndexHashTable.FullFastIterator, and since the code is identical, it's probably also broken.

      Attachments

        Activity

          People

            mproctor@redhat.com Mark Proctor
            hltbdivl_jira Adar Dembo (Inactive)
            Archiver:
            rhn-support-ceverson Clark Everson

            Dates

              Created:
              Updated:
              Resolved:
              Archived:

              PagerDuty