Skip to content

Commit 8d2244b

Browse files
andyparkinsgitster
authored andcommitted
Make git-prune submodule aware (and fix a SEGFAULT in the process)
I ran git-prune on a repository and got this: $ git-prune error: Object 228f8065b930120e35fc0c154c237487ab02d64a is a blob, not a commit Segmentation fault (core dumped) This repository was a strange one in that it was being used to provide its own submodule. That is, the repository was cloned into a subdirectory, an independent branch checked out in that subdirectory, and then it was marked as a submodule. git-prune then failed in the above manner. The problem was that git-prune was not submodule aware in two areas. Linus said: > So what happens is that something traverses a tree object, looks at each > entry, sees that it's not a tree, and tries to look it up as a blob. But > subprojects are commits, not blobs, and then when you look at the object > more closely, you get the above kind of object type confusion. and included a patch to add an S_ISGITLINK() test to reachable.c's process_tree() function. That fixed the first git-prune error, and stopped it from trying to process the gitlink entries in trees as if they were pointers to other trees (and of course failing, because gitlinks _aren't_ trees). That part of this patch is his. The second area is add_cache_refs(). This is called before starting the reachability analysis, and was calling lookup_blob() on every object hash found in the index. However, it is no longer true that every hash in the index is a pointer to a blob, some of them are gitlinks, and are not backed by any object at all, they are commits in another repository. Normally this bug was not causing any problems, but in the case of the self-referencing repository described above, it meant that the gitlink hash was being marked as being of type OBJ_BLOB by add_cache_refs() call to lookup_blob(). Then later, because that hash was also pointed to by a ref, add_one_ref() would treat it as a commit; lookup_commit() would return a NULL because that object was already noted as being an OBJ_BLOB, not an OBJ_COMMIT; and parse_commit_buffer() would SEGFAULT on that NULL pointer. The fix made by this patch is to not blindly call lookup_blob() in reachable.c's add_cache_refs(), and instead skip any index entries that are S_ISGITLINK(). Signed-off-by: Andy Parkins <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5788744 commit 8d2244b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

reachable.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ static void process_blob(struct blob *blob,
2121
/* Nothing to do, really .. The blob lookup was the important part */
2222
}
2323

24+
static void process_gitlink(const unsigned char *sha1,
25+
struct object_array *p,
26+
struct name_path *path,
27+
const char *name)
28+
{
29+
/* I don't think we want to recurse into this, really. */
30+
}
31+
2432
static void process_tree(struct tree *tree,
2533
struct object_array *p,
2634
struct name_path *path,
@@ -47,6 +55,8 @@ static void process_tree(struct tree *tree,
4755
while (tree_entry(&desc, &entry)) {
4856
if (S_ISDIR(entry.mode))
4957
process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
58+
else if (S_ISDIRLNK(entry.mode))
59+
process_gitlink(entry.sha1, p, &me, entry.path);
5060
else
5161
process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
5262
}
@@ -159,6 +169,16 @@ static void add_cache_refs(struct rev_info *revs)
159169

160170
read_cache();
161171
for (i = 0; i < active_nr; i++) {
172+
/*
173+
* The index can contain blobs and GITLINKs, GITLINKs are hashes
174+
* that don't actually point to objects in the repository, it's
175+
* almost guaranteed that they are NOT blobs, so we don't call
176+
* lookup_blob() on them, to avoid populating the hash table
177+
* with invalid information
178+
*/
179+
if (S_ISDIRLNK(ntohl(active_cache[i]->ce_mode)))
180+
continue;
181+
162182
lookup_blob(active_cache[i]->sha1);
163183
/*
164184
* We could add the blobs to the pending list, but quite

0 commit comments

Comments
 (0)