cleanup: fix possible overflow errors in binary search
A common mistake when writing binary search is to allow possible integer overflow by using the simple average: mid = (min + max) / 2; Instead, use the overflow-safe version: mid = min + (max - min) / 2; This translation is safe since the operation occurs inside a loop conditioned on "min < max". The included changes were found using the following git grep: git grep '/ *2;' '*.c' Making this cleanup will prevent future review friction when a new binary search is contructed based on existing code. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
217f2767cb
commit
19716b21a4
@ -633,7 +633,7 @@ static int find_ofs_delta(const off_t offset, enum object_type type)
|
|||||||
int first = 0, last = nr_ofs_deltas;
|
int first = 0, last = nr_ofs_deltas;
|
||||||
|
|
||||||
while (first < last) {
|
while (first < last) {
|
||||||
int next = (first + last) / 2;
|
int next = first + (last - first) / 2;
|
||||||
struct ofs_delta_entry *delta = &ofs_deltas[next];
|
struct ofs_delta_entry *delta = &ofs_deltas[next];
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
@ -687,7 +687,7 @@ static int find_ref_delta(const unsigned char *sha1, enum object_type type)
|
|||||||
int first = 0, last = nr_ref_deltas;
|
int first = 0, last = nr_ref_deltas;
|
||||||
|
|
||||||
while (first < last) {
|
while (first < last) {
|
||||||
int next = (first + last) / 2;
|
int next = first + (last - first) / 2;
|
||||||
struct ref_delta_entry *delta = &ref_deltas[next];
|
struct ref_delta_entry *delta = &ref_deltas[next];
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
|
@ -1277,7 +1277,7 @@ static int done_pbase_path_pos(unsigned hash)
|
|||||||
int lo = 0;
|
int lo = 0;
|
||||||
int hi = done_pbase_paths_num;
|
int hi = done_pbase_paths_num;
|
||||||
while (lo < hi) {
|
while (lo < hi) {
|
||||||
int mi = (hi + lo) / 2;
|
int mi = lo + (hi - lo) / 2;
|
||||||
if (done_pbase_paths[mi] == hash)
|
if (done_pbase_paths[mi] == hash)
|
||||||
return mi;
|
return mi;
|
||||||
if (done_pbase_paths[mi] < hash)
|
if (done_pbase_paths[mi] < hash)
|
||||||
|
@ -394,7 +394,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
|
|||||||
lo = 0;
|
lo = 0;
|
||||||
hi = nr;
|
hi = nr;
|
||||||
while (lo < hi) {
|
while (lo < hi) {
|
||||||
mid = (lo + hi)/2;
|
mid = lo + (hi - lo) / 2;
|
||||||
if (base_offset < obj_list[mid].offset) {
|
if (base_offset < obj_list[mid].offset) {
|
||||||
hi = mid;
|
hi = mid;
|
||||||
} else if (base_offset > obj_list[mid].offset) {
|
} else if (base_offset > obj_list[mid].offset) {
|
||||||
|
@ -49,7 +49,7 @@ static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
|
|||||||
lo = 0;
|
lo = 0;
|
||||||
hi = it->subtree_nr;
|
hi = it->subtree_nr;
|
||||||
while (lo < hi) {
|
while (lo < hi) {
|
||||||
int mi = (lo + hi) / 2;
|
int mi = lo + (hi - lo) / 2;
|
||||||
struct cache_tree_sub *mdl = down[mi];
|
struct cache_tree_sub *mdl = down[mi];
|
||||||
int cmp = subtree_name_cmp(path, pathlen,
|
int cmp = subtree_name_cmp(path, pathlen,
|
||||||
mdl->name, mdl->namelen);
|
mdl->name, mdl->namelen);
|
||||||
|
@ -613,7 +613,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
|
|||||||
int low = 0, high = pstr->valid_len, mid;
|
int low = 0, high = pstr->valid_len, mid;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
mid = (high + low) / 2;
|
mid = low + (high - low) / 2;
|
||||||
if (pstr->offsets[mid] > offset)
|
if (pstr->offsets[mid] > offset)
|
||||||
high = mid;
|
high = mid;
|
||||||
else if (pstr->offsets[mid] < offset)
|
else if (pstr->offsets[mid] < offset)
|
||||||
@ -1394,7 +1394,7 @@ re_node_set_contains (const re_node_set *set, int elem)
|
|||||||
right = set->nelem - 1;
|
right = set->nelem - 1;
|
||||||
while (idx < right)
|
while (idx < right)
|
||||||
{
|
{
|
||||||
mid = (idx + right) / 2;
|
mid = idx + (right - idx) / 2;
|
||||||
if (set->elems[mid] < elem)
|
if (set->elems[mid] < elem)
|
||||||
idx = mid + 1;
|
idx = mid + 1;
|
||||||
else
|
else
|
||||||
|
@ -4284,7 +4284,7 @@ search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
|
|||||||
last = right = mctx->nbkref_ents;
|
last = right = mctx->nbkref_ents;
|
||||||
for (left = 0; left < right;)
|
for (left = 0; left < right;)
|
||||||
{
|
{
|
||||||
mid = (left + right) / 2;
|
mid = left + (right - left) / 2;
|
||||||
if (mctx->bkref_ents[mid].str_idx < str_idx)
|
if (mctx->bkref_ents[mid].str_idx < str_idx)
|
||||||
left = mid + 1;
|
left = mid + 1;
|
||||||
else
|
else
|
||||||
|
@ -1743,7 +1743,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
|
|||||||
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
|
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
|
||||||
|
|
||||||
while (lo < hi) {
|
while (lo < hi) {
|
||||||
unsigned mi = (lo + hi) / 2;
|
unsigned mi = lo + (hi - lo) / 2;
|
||||||
int cmp = hashcmp(index + mi * stride, sha1);
|
int cmp = hashcmp(index + mi * stride, sha1);
|
||||||
|
|
||||||
if (debug_lookup)
|
if (debug_lookup)
|
||||||
|
@ -10,7 +10,7 @@ static uint32_t take2(const unsigned char *sha1)
|
|||||||
* Conventional binary search loop looks like this:
|
* Conventional binary search loop looks like this:
|
||||||
*
|
*
|
||||||
* do {
|
* do {
|
||||||
* int mi = (lo + hi) / 2;
|
* int mi = lo + (hi - lo) / 2;
|
||||||
* int cmp = "entry pointed at by mi" minus "target";
|
* int cmp = "entry pointed at by mi" minus "target";
|
||||||
* if (!cmp)
|
* if (!cmp)
|
||||||
* return (mi is the wanted one)
|
* return (mi is the wanted one)
|
||||||
@ -95,7 +95,7 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
|
|||||||
hi = mi;
|
hi = mi;
|
||||||
else
|
else
|
||||||
lo = mi + 1;
|
lo = mi + 1;
|
||||||
mi = (hi + lo) / 2;
|
mi = lo + (hi - lo) / 2;
|
||||||
} while (lo < hi);
|
} while (lo < hi);
|
||||||
return -lo-1;
|
return -lo-1;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ static void unique_in_pack(struct packed_git *p,
|
|||||||
num = p->num_objects;
|
num = p->num_objects;
|
||||||
last = num;
|
last = num;
|
||||||
while (first < last) {
|
while (first < last) {
|
||||||
uint32_t mid = (first + last) / 2;
|
uint32_t mid = first + (last - first) / 2;
|
||||||
const unsigned char *current;
|
const unsigned char *current;
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ static int get_entry_index(const struct string_list *list, const char *string,
|
|||||||
compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
|
compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
|
||||||
|
|
||||||
while (left + 1 < right) {
|
while (left + 1 < right) {
|
||||||
int middle = (left + right) / 2;
|
int middle = left + (right - left) / 2;
|
||||||
int compare = cmp(string, list->items[middle].string);
|
int compare = cmp(string, list->items[middle].string);
|
||||||
if (compare < 0)
|
if (compare < 0)
|
||||||
right = middle;
|
right = middle;
|
||||||
|
2
utf8.c
2
utf8.c
@ -32,7 +32,7 @@ static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
|
|||||||
if (ucs < table[0].first || ucs > table[max].last)
|
if (ucs < table[0].first || ucs > table[max].last)
|
||||||
return 0;
|
return 0;
|
||||||
while (max >= min) {
|
while (max >= min) {
|
||||||
mid = (min + max) / 2;
|
mid = min + (max - min) / 2;
|
||||||
if (ucs > table[mid].last)
|
if (ucs > table[mid].last)
|
||||||
min = mid + 1;
|
min = mid + 1;
|
||||||
else if (ucs < table[mid].first)
|
else if (ucs < table[mid].first)
|
||||||
|
@ -166,7 +166,7 @@ static int binary_search(struct entry **sequence, int longest,
|
|||||||
int left = -1, right = longest;
|
int left = -1, right = longest;
|
||||||
|
|
||||||
while (left + 1 < right) {
|
while (left + 1 < right) {
|
||||||
int middle = (left + right) / 2;
|
int middle = left + (right - left) / 2;
|
||||||
/* by construction, no two entries can be equal */
|
/* by construction, no two entries can be equal */
|
||||||
if (sequence[middle]->line2 > entry->line2)
|
if (sequence[middle]->line2 > entry->line2)
|
||||||
right = middle;
|
right = middle;
|
||||||
|
Loading…
Reference in New Issue
Block a user