Fix intset midpoint selection

The classic (min+max)/2 is provably unsafe.  Fixed
as recommended in research:
http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

Fix inspired by @wjin, but I used a different approach.
(later, I found @kuebler fixed the same issue too).

Fixes #1741, #1602
This commit is contained in:
Matt Stancliff 2014-08-01 13:01:33 -04:00 committed by antirez
parent 87815ab5ba
commit c82e0b701f

View File

@ -133,7 +133,7 @@ static uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {
} }
while(max >= min) { while(max >= min) {
mid = (min+max)/2; mid = ((unsigned int)min + (unsigned int)max) >> 1;
cur = _intsetGet(is,mid); cur = _intsetGet(is,mid);
if (value > cur) { if (value > cur) {
min = mid+1; min = mid+1;