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

Fix git-fsck-objects SIGSEGV/divide-by-zero

From
Linus Torvalds <torvalds@osdl.org>
Date
Sep 4, 2006, 15:34 UTC
Message-ID
<Pine.LNX.4.64.0609040821520.27779@g5.osdl.org>

If you try to fsck a repository that isn't entirely empty, but that has no inter-object references (ie all the objects are blobs, and don't refer to anything else), git-fsck-objects currently fails.

This probably cannot happen in practice, but can be tested with something like

	git init-db
	touch dummy
	git add dummy
	git fsck-objects

where the fsck will die by a divide-by-zero when it tries to look up the references from the one object it found (hash_obj() will do a modulus by refs_hash_size).

On some other archiectures (ppc, sparc) the divide-by-zero will go unnoticed, and we'll instead SIGSEGV when we hit the "refs_hash[j]" access.

So move the test that should protect against this from mark_reachable() into lookup_object_refs(), which incidentally in the process also fixes mark_reachable() itself (it used to not mark the one object that _was_ reachable, because it decided that it had no refs too early).

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/object-refs.c b/object-refs.c
index b0034e4..98ea100 100644
--- a/object-refs.c
+++ b/object-refs.c
@@ -55,9 +55,13 @@ static void add_object_refs(struct objec
 
 struct object_refs *lookup_object_refs(struct object *obj)
 {
-	int j = hash_obj(obj, refs_hash_size);
 	struct object_refs *ref;
+	int j;
 
+	/* nothing to lookup */
+	if (!refs_hash_size)
+		return NULL;
+	j = hash_obj(obj, refs_hash_size);
 	while ((ref = refs_hash[j]) != NULL) {
 		if (ref->base == obj)
 			break;
@@ -125,9 +129,6 @@ void mark_reachable(struct object *obj, 
 
 	if (!track_object_refs)
 		die("cannot do reachability with object refs turned off");
-	/* nothing to lookup */
-	if (!refs_hash_size)
-		return;
 	/* If we've been here already, don't bother */
 	if (obj->flags & mask)
 		return;
Message 1 of 1 in “Fix git-fsck-objects SIGSEGV/divide-by-zero”
  1. Linus TorvaldsSep 4, 2006

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.