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

Re: [PATCH] read-cache.c: fix index memory allocation

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 24, 2011, 07:28 UTC
Message-ID
<7vaa8q4zm9.fsf@alter.siamese.dyndns.org>
In-Reply-To
<4EA4B8E7.5070106@lsrfire.ath.cx>
René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:
>  t/t7510-status-index.sh |   50 +++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 53 insertions(+), 3 deletions(-)
>  create mode 100755 t/t7510-status-index.sh
Show 6 quoted lines
> diff --git a/t/t7510-status-index.sh b/t/t7510-status-index.sh
> new file mode 100755
> index 0000000..bca359d
> --- /dev/null
> +++ b/t/t7510-status-index.sh
> @@ -0,0 +1,50 @@

Hmm, I cannot seem to make this fail this test without the fix on my Fedora 14 i686 VM when applied to v1.7.6.4 (estimation code originates cf55870 back in v1.7.6.1 days), but it does break on 'master'.

By the way, I'll move this to 7511.
Also would a patch like this help?
-- >8 --
Subject: [PATCH] read_index(): die on estimation error
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 read-cache.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
diff --git a/read-cache.c b/read-cache.c
index 0a64103..2926615 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1270,6 +1270,7 @@ int read_index_from(struct index_state *istate, const char *path)
 	int fd, i;
 	struct stat st;
 	unsigned long src_offset, dst_offset;
+	size_t bulk_alloc_size;
 	struct cache_header *hdr;
 	void *mmap;
 	size_t mmap_size;
@@ -1315,7 +1316,8 @@ int read_index_from(struct index_state *istate, const char *path)
 	 * has room for a few  more flags, we can allocate using the same
 	 * index size
 	 */
-	istate->alloc = xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));
+	bulk_alloc_size = estimate_cache_size(mmap_size, istate->cache_nr);
+	istate->alloc = xmalloc(bulk_alloc_size);
 	istate->initialized = 1;
 
 	src_offset = sizeof(*hdr);
@@ -1331,7 +1333,9 @@ int read_index_from(struct index_state *istate, const char *path)
 
 		src_offset += ondisk_ce_size(ce);
 		dst_offset += ce_size(ce);
+		if (bulk_alloc_size <= dst_offset)
+			die("cache size estimation error");
 	}
 	istate->timestamp.sec = st.st_mtime;
 	istate->timestamp.nsec = ST_MTIME_NSEC(st);
 
-- 
1.7.7.1.504.gcc718
Previous: Junio C HamanoNext: René Scharfe
Message 16 of 17 in “a bug when execute "git status" in git version 1.7.7.431.g89633”
  1. John HsingOct 22, 2011
  2. Matthieu MoyOct 23, 2011
  3. John HsingOct 23, 2011
  4. René ScharfeOct 23, 2011
  5. René ScharfeOct 23, 2011
  6. Jeff KingOct 23, 2011
  7. René ScharfeOct 23, 2011
  8. read-cache.c: fix index memory allocationRené Scharfe, Oct 24, 2011
  9. Junio C HamanoOct 24, 2011
  10. René ScharfeOct 24, 2011
  11. René ScharfeOct 24, 2011
  12. Nguyen Thai Ngoc DuyOct 24, 2011
  13. Nguyen Thai Ngoc DuyOct 25, 2011
  14. René ScharfeOct 25, 2011
  15. Junio C HamanoOct 25, 2011
  16. Junio C HamanoOct 24, 2011
  17. René ScharfeOct 24, 2011

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.