summaryrefslogtreecommitdiff
path: root/read-cache.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2019-06-13 17:51:56 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-06-13 18:28:53 (GMT)
commit568a05c5ecb8e3a01fcb90d0f81857f49ef2add8 (patch)
treee85ff4b06b04fd9427a417c7c7b41ad73c0bd6dc /read-cache.c
parentb697d92f56511e804b8ba20ccbe7bdc85dc66810 (diff)
downloadgit-568a05c5ecb8e3a01fcb90d0f81857f49ef2add8.zip
git-568a05c5ecb8e3a01fcb90d0f81857f49ef2add8.tar.gz
git-568a05c5ecb8e3a01fcb90d0f81857f49ef2add8.tar.bz2
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 'read-cache.c')
-rw-r--r--read-cache.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/read-cache.c b/read-cache.c
index 22e7b99..4f81fca 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -549,7 +549,7 @@ static int index_name_stage_pos(const struct index_state *istate, const char *na
first = 0;
last = istate->cache_nr;
while (last > first) {
- int next = (last + first) >> 1;
+ int next = first + ((last - first) >> 1);
struct cache_entry *ce = istate->cache[next];
int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
if (!cmp)