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

[PATCH] Must not modify the_index.cache as it may be passed to realloc at some point.

From
Keith Packard <keithp@keithp.com>
Date
Oct 3, 2007, 05:44 UTC
Message-ID
<1191390255.16292.2.camel@koto.keithp.com>

The index cache is not static, growing as new entries are added. If entries are added after prune_cache is called, cache will no longer point at the base of the allocation, and realloc will not be happy.

I verified that this was the only place in the current source which modified any index_state.cache elements aside from the alloc/realloc calls in read-cache by changing the type of the element to 'struct cache_entry ** const cache' and recompiling.

A more efficient patch would create a separate 'cache_base' value to track
the allocation and then fix things up when reallocation was necessary,
instead of the brute-force memmove used here.
---
 builtin-ls-files.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index 6c1db86..0028b8a 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -280,7 +280,7 @@ static void prune_cache(const char *prefix)
 
        if (pos < 0)
                pos = -pos-1;
-       active_cache += pos;
+       memmove (active_cache, active_cache + pos, (active_nr - pos) * sizeof (struct cache_entry *));
        active_nr -= pos;
        first = 0;
        last = active_nr;
-- 
1.5.3.3.131.g34c6d-dirty

-- 
keith.packard@intel.com
Next: Junio C Hamano
Message 1 of 14 in “Must not modify the_index.cache as it may be passed to realloc at some point.”
  1. Must not modify the_index.cache as it may be passed to realloc at some point.Keith Packard, Oct 3, 2007
  2. Junio C HamanoOct 3, 2007
  3. Add test case for ls-files --with-headCarl Worth, Oct 3, 2007
  4. Johannes SixtOct 3, 2007
  5. Johannes SchindelinOct 3, 2007
  6. David KastrupOct 3, 2007
  7. Carl WorthOct 3, 2007
  8. David KastrupOct 3, 2007
  9. Jeff KingOct 3, 2007
  10. Johannes SchindelinOct 3, 2007
  11. Junio C HamanoOct 3, 2007
  12. Jeff KingOct 3, 2007
  13. Junio C HamanoOct 3, 2007
  14. Add test case for ls-files --with-headCarl Worth, Oct 3, 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.