Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2019-06-13 20:51:56 +0300
committerJunio C Hamano <gitster@pobox.com>2019-06-13 21:28:53 +0300
commit568a05c5ecb8e3a01fcb90d0f81857f49ef2add8 (patch)
treee85ff4b06b04fd9427a417c7c7b41ad73c0bd6dc /name-hash.c
parentb697d92f56511e804b8ba20ccbe7bdc85dc66810 (diff)
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>
Diffstat (limited to 'name-hash.c')
-rw-r--r--name-hash.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/name-hash.c b/name-hash.c
index b4861bc7b0..695908609f 100644
--- a/name-hash.c
+++ b/name-hash.c
@@ -345,8 +345,9 @@ static int handle_range_dir(
else {
int begin = k_start;
int end = k_end;
+ assert(begin >= 0);
while (begin < end) {
- int mid = (begin + end) >> 1;
+ int mid = begin + ((end - begin) >> 1);
int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len);
if (cmp == 0) /* mid has same prefix; look in second part */
begin = mid + 1;