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

Re: [PATCH] sha1_name: make wraparound of the index into ring-buffer explicit

From
Jeff King <peff@peff.net>
Date
Nov 1, 2016, 15:04 UTC
Message-ID
<20161101150429.fohpchtlfvohzood@sigill.intra.peff.net>
In-Reply-To
<a1e2c4a4-4720-fc26-c866-6ab959543c3b@web.de>
On Tue, Nov 01, 2016 at 09:49:07AM +0100, René Scharfe wrote:
> Overflow is defined for unsigned integers, but not for signed ones.
> Wrap around explicitly for the new ring-buffer in find_unique_abbrev()
> as we did in bb84735c for the ones in sha1_to_hex() and get_pathname(),
> thus avoiding signed overflows and getting rid of the magic number 3.

Thanks. I didn't mean to make you do this, but my procrastination won again.

Show 11 quoted lines
> diff --git a/sha1_name.c b/sha1_name.c
> index 06409a3..73a915f 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -510,7 +510,8 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
>  {
>  	static int bufno;
>  	static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
> -	char *hex = hexbuffer[3 & ++bufno];
> +	char *hex = hexbuffer[bufno];
> +	bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
Code looks obviously correct (and it even does the "start at 0" thing).
-Peff
Previous: René Scharfe
Message 2 of 2 in “sha1_name: make wraparound of the index into ring-buffer explicit”
  1. sha1_name: make wraparound of the index into ring-buffer explicitRené Scharfe, Nov 1, 2016
  2. Jeff KingNov 1, 2016

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.