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

[PATCH 2/2] fix "bundle --stdin" segfault

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 19, 2010, 08:03 UTC
Message-ID
<20100419080340.GB23609@progeny.tock>
In-Reply-To
<20100419071449.GA23166@progeny.tock>

When passed an empty list, objects_array_remove_duplicates() corrupts it by changing the number of entries from 0 to 1.

The problem lies in the condition of its main loop:
	for (ref = 0; ref < array->nr - 1; ref++) {

The loop body manipulates the supplied object array. In the case of an empty array, it should not be doing anything at all. But array->nr is an unsigned quantity, so the code enters the loop, in particular increasing array->nr. Fix this by comparing (ref + 1 < array->nr) instead.

This bug can be triggered by git bundle --stdin:
	$ echo HEAD | git bundle create some.bundle --stdin’
	Segmentation fault (core dumped)

The list of commits to bundle appears to be empty because of another bug: by the time the revision-walking machinery gets to look at it, standard input has already been consumed by rev-list, so ...remove_duplicates() gets an empty list of revisions.

After this patch, git bundle --stdin still does not work; it just doesn’t segfault any more.

Reported-by: Joey Hess <joey@kitenet.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 object.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/object.c b/object.c
index 3ca92c4..277b3dd 100644
--- a/object.c
+++ b/object.c
@@ -252,10 +252,10 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
 
 void object_array_remove_duplicates(struct object_array *array)
 {
-	int ref, src, dst;
+	unsigned int ref, src, dst;
 	struct object_array_entry *objects = array->objects;
 
-	for (ref = 0; ref < array->nr - 1; ref++) {
+	for (ref = 0; ref + 1 < array->nr; ref++) {
 		for (src = ref + 1, dst = src;
 		     src < array->nr;
 		     src++) {
-- 
1.7.1.rc1
Previous: Jonathan NiederNext: Junio C Hamano
Message 5 of 19 in “bug: git-bundle create foo --stdin -> segfault”
  1. Joey HessJan 19, 2010
  2. Johannes SchindelinJan 19, 2010
  3. 0/2 test bundle --stdin, fix objects_array_remove_duplicates()Jonathan Nieder, Apr 19, 2010
  4. 1/2 t5704 (bundle): add tests for bundle --stdinJonathan Nieder, Apr 19, 2010
  5. 2/2 fix "bundle --stdin" segfaultJonathan Nieder, Apr 19, 2010
  6. Junio C HamanoApr 20, 2010
  7. 0/8 Re: bug: git-bundle create foo --stdin -> segfaultJonathan Nieder, Jun 26, 2010
  8. 1/8 bundle: split basis discovery into its own functionJonathan Nieder, Jun 26, 2010
  9. 2/8 bundle: use libified rev-list --boundaryJonathan Nieder, Jun 26, 2010
  10. Junio C HamanoJun 30, 2010
  11. Jonathan NiederJun 30, 2010
  12. 3/8 bundle: give list_prerequisites() loop body its own functionJonathan Nieder, Jun 26, 2010
  13. Junio C HamanoJun 30, 2010
  14. Jonathan NiederJun 30, 2010
  15. 4/8 bundle: split table of contents output into its own functionJonathan Nieder, Jun 26, 2010
  16. 5/8 bundle: reuse setup_revisions resultJonathan Nieder, Jun 26, 2010
  17. 6/8 Fix bundle --stdinJonathan Nieder, Jun 26, 2010
  18. 7/8 bundle: Keep names of basis refs after discoveryJonathan Nieder, Jun 26, 2010
  19. 8/8 bundle_create: Do not exit when given no revs to bundleJonathan Nieder, Jun 26, 2010

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.