Use proper comparisons while traversing the history list.
Use proper comparisons while traversing the history list.

The index and the length of the list both need to be stored in an
unsigned type.
Also, the comparison i <= length - 1 should be i < length, in case
length is 0.

razr/opentts,razr/opentts,razr/opentts,razr/opentts