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

Re: Fix up ugly open-coded "alloc_nr()" user in object.c

From
Jeff King <peff@peff.net>
Date
Jun 16, 2007, 22:37 UTC
Message-ID
<20070616223738.GA19076@coredump.intra.peff.net>
In-Reply-To
<20070616221506.GA78651@dspnet.fr.eu.org>
On Sun, Jun 17, 2007 at 12:15:06AM +0200, Olivier Galibert wrote:
> > +	ALLOC_GROW(array->objects, array->nr, array->alloc);
> 
> Unless the ALLOC_GROW semantics are weird, shouldn't that be:
>   ALLOC_GROW(array->objects, array->nr+1, array->alloc);

The semantics are weird. They never seemed so to me before, since it was replacing some "grow by 1" areas where it is natural to assume that you need just one spot more. But the way Junio commented it and tweaked it, it can handle arbitrary growth (which is much better), but that means we are overly conservative about when to grow.

Junio, patch is below (call-sites using bare 'nr' need to be 'nr+1', but I will fix those up in a separate patch since they are in next and this is in master).

-- >8 -- fix ALLOC_GROW off-by-one

The ALLOC_GROW macro will never let us fill the array completely, instead allocating an extra chunk if that would be the case. This is because the 'nr' argument was originally treated as "how much we do have now" instead of "how much do we want". The latter makes much more sense because you can grow by more than one item.

This off-by-one never resulted in an error because it meant we were overly conservative about when to allocate. Any callers which passed "how we have now" need to be updated, or they will fail to allocate enough.

Signed-off-by: Jeff King <peff@peff.net>
---
 cache.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/cache.h b/cache.h
index c914c1c..ed83d92 100644
--- a/cache.h
+++ b/cache.h
@@ -234,7 +234,7 @@ extern void verify_non_filename(const char *prefix, const char *name);
  */
 #define ALLOC_GROW(x, nr, alloc) \
 	do { \
-		if ((nr) >= alloc) { \
+		if ((nr) > alloc) { \
 			if (alloc_nr(alloc) < (nr)) \
 				alloc = (nr); \
 			else \
Previous: Olivier Galibert
Message 4 of 4 in “Fix up ugly open-coded "alloc_nr()" user in object.c”
  1. Linus TorvaldsJun 16, 2007
  2. Jeff KingJun 16, 2007
  3. Olivier GalibertJun 16, 2007
  4. Jeff KingJun 16, 2007

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.