cleanup: fix possible overflow errors in binary search, part 2

Calculating the sum of two array indexes to find the midpoint between them can overflow, i.e. code like this is unsafe for big arrays: mid = (first + last) >> 1; Make sure the intermediate value stays within the boundaries instead, like this: mid = first + ((last - first) >> 1); The loop condition of the binary search makes sure that 'last' is always greater than 'first', so this is safe as long as 'first' is not negative. And that can be verified easily using the pre-context of each change, except for name-hash.c, so add an assertion to that effect there. The unsafe calculations were found with: git grep '(.*+.*) *>> *1' This is a continuation of 19716b21a4 (cleanup: fix possible overflow errors in binary search, 2017-10-08). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 13, 2019 at 19:51 UTC 568a05c5ecb8e3a01fcb90d0f81857f49ef2add8
6 files changed +8 -7
builtin/ls-files.c
+1 -1
@@ -373,7 +373,7 @@ static void prune_index(struct index_state *istate,
373 first = pos;
374 last = istate->cache_nr;
375 while (last > first) {
376 - int next = (last + first) >> 1;
376 + int next = first + ((last - first) >> 1);
377 const struct cache_entry *ce = istate->cache[next];
378 if (!strncmp(ce->name, prefix, prefixlen)) {
379 first = next+1;
diffcore-rename.c
+2 -2
@@ -23,7 +23,7 @@ static int find_rename_dst(struct diff_filespec *two)
23 first = 0;
24 last = rename_dst_nr;
25 while (last > first) {
26 - int next = (last + first) >> 1;
26 + int next = first + ((last - first) >> 1);
27 struct diff_rename_dst *dst = &(rename_dst[next]);
28 int cmp = strcmp(two->path, dst->two->path);
29 if (!cmp)
@@ -83,7 +83,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
83 first = 0;
84 last = rename_src_nr;
85 while (last > first) {
86 - int next = (last + first) >> 1;
86 + int next = first + ((last - first) >> 1);
87 struct diff_rename_src *src = &(rename_src[next]);
88 int cmp = strcmp(one->path, src->p->one->path);
89 if (!cmp)
dir.c
+1 -1
@@ -701,7 +701,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
701 first = 0;
702 last = dir->dirs_nr;
703 while (last > first) {
704 - int cmp, next = (last + first) >> 1;
704 + int cmp, next = first + ((last - first) >> 1);
705 d = dir->dirs[next];
706 cmp = strncmp(name, d->name, len);
707 if (!cmp && strlen(d->name) > len)
name-hash.c
+2 -1
@@ -345,8 +345,9 @@ static int handle_range_dir(
345 else {
346 int begin = k_start;
347 int end = k_end;
348 + assert(begin >= 0);
349 while (begin < end) {
349 - int mid = (begin + end) >> 1;
350 + int mid = begin + ((end - begin) >> 1);
351 int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len);
352 if (cmp == 0) /* mid has same prefix; look in second part */
353 begin = mid + 1;
read-cache.c
+1 -1
@@ -549,7 +549,7 @@ static int index_name_stage_pos(const struct index_state *istate, const char *na
549 first = 0;
550 last = istate->cache_nr;
551 while (last > first) {
552 - int next = (last + first) >> 1;
552 + int next = first + ((last - first) >> 1);
553 struct cache_entry *ce = istate->cache[next];
554 int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
555 if (!cmp)
sh-i18n--envsubst.c
+1 -1
@@ -249,7 +249,7 @@ sorted_string_list_member (const string_list_ty *slp, const char *s)
249 {
250 /* Here we know that if s is in the list, it is at an index j
251 with j1 <= j < j2. */
252 - size_t j = (j1 + j2) >> 1;
252 + size_t j = j1 + ((j2 - j1) >> 1);
253 int result = strcmp (slp->item[j], s);
254
255 if (result > 0)