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
MÅMartin Ågren <martin.agren@gmail.com>
Date
Jun 14, 2019, 04:18 UTC
Message-ID
<CAN0heSrEK80XHFYYuXhb4rg76Xv6n7-p-oUdyLH39vpAfjkxZg@mail.gmail.com>
In-Reply-To
<1473675e-4d27-8221-0e7a-f88eb31ee11f@web.de>
On Thu, 13 Jun 2019 at 23:33, René Scharfe <l.s.r@web.de> wrote:
>
> Am 13.06.19 um 21:42 schrieb Martin Ågren:
> > On Thu, 13 Jun 2019 at 19:54, René Scharfe <l.s.r@web.de> wrote:
Show 10 quoted lines
> >> 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.
Show 10 quoted lines
> > So all is well. But maybe we should write `(last - first) / 2` anyway.
> > We could then drop the extra parenthesis, and we would keep future
> > readers (and static analysis?) from wondering whether we might ever be
> > shifting a signed value with the sign bit set. A few spots fewer to
> > audit in the future...
>
> Yes, thought about that.  When I saw Clang 8 emitting extra opcodes for
> handling the sign for the version with division I decided to restrict
> the patch to just do overflow prevention and leave the right shifts in
> place..

Ah, ok, I should have known you were on top of it. I guess that means that clang isn't able to figure out we're guaranteed to be working with non-negative numbers. Which I guess means that it can't be sure the shift is safe, but with undefined behavior it has the leeway it needs, so...

Martin
Previous: René Scharfe
Message 5 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.