git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] cleanup: fix possible overflow errors in binary search, part 2

From
Derrick Stolee <stolee@gmail.com>
Date
Jun 13, 2019, 18:06 UTC
Message-ID
<ac09e504-369d-d6d5-a356-07a75386d3fb@gmail.com>
In-Reply-To
<f0059613-d3e4-a1e2-2c01-727579e069d2@web.de>
On 6/13/2019 1:51 PM, René Scharfe wrote:
Show 22 quoted lines
> 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).
Thank you for finding these additional examples!
LGTM. Pretty easy to see this is correct.
-Stolee
Previous: René ScharfeNext: Martin Ågren
Message 2 of 5 in “cleanup: fix possible overflow errors in binary search, part 2”
  1. cleanup: fix possible overflow errors in binary search, part 2René Scharfe, Jun 13, 2019
  2. Derrick StoleeJun 13, 2019
  3. Martin ÅgrenJun 13, 2019
  4. René ScharfeJun 13, 2019
  5. Martin ÅgrenJun 14, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.