Bugs in KComboBox

Andrew Coles andrew_coles at yahoo.co.uk
Thu Nov 11 15:40:26 GMT 2004


Whilst reading through KComboBox.cpp I spotted what I think look like a few 
bugs.  Wanting to be sure that my understanding is correct, and not being 
arrogant enough to risk breaking lots of existing code (given it's used all 
over the place), I've detailed them here along with my fixes; if they're OKed 
I'll commit.

1) From KHistoryCombo::addToHistory, line 450

This code will, if I understand correctly, will only remove half the entries 
in the list, as the call to removeItem(i) will shuffle the indices for the 
rest of the entries backwards one.

for ( int i = 0; i < count(); i++ ) {
 if ( text( i ) == item ) {
  if ( !wasCurrent )
   wasCurrent = ( i == currentItem() );
  removeItem( i );
 }
}

Solution - ditch the call to removeItem(i) and call clear() after the for loop 
has completed.

2) From KHistoryCombo::removeFromHistory, line 494

This code will only remove the first of two adjacent matches if they exist, 
leaving the second in the list - again, the remaining indices are shuffled 
backwards one for each removal.

for ( int i = 0; i < count(); i++ ) {
 while ( item == text( i ) ) {
  removed = true;
  removeItem( i );
 }
}

Solution: replace the for loop with a while loop, only incrementing i if an 
item has not been removed, and replace the while with an if statement.

Andrew




More information about the kde-core-devel mailing list