Skip to content
  • René Scharfe's avatar
    cleanup: fix possible overflow errors in binary search, part 2 · 568a05c5
    René Scharfe authored and Junio C Hamano's avatar Junio C Hamano committed
    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 19716b21
    
     (cleanup: fix possible overflow
    errors in binary search, 2017-10-08).
    
    Signed-off-by: default avatarRene Scharfe <l.s.r@web.de>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    568a05c5