Skip to content

Commit 5fb8c05

Browse files
committed
get_tree_entry(): do not call find_tree_entry() on an empty tree
We know we will find nothing. This incidentally squelches false warning from gcc about potentially uninitialized usage of t.entry fields. For an empty tree, it is true that init_tree_desc() does not call decode_tree_entry() and the tree_desc is left uninitialized, but find_tree_entry() only calls tree_entry_extract() that uses the tree_desc while it has more things to read from the tree, so the uninitialized t.entry fields are never used in such a case anyway. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0de1633 commit 5fb8c05

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

tree-walk.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
465465
int retval;
466466
void *tree;
467467
unsigned long size;
468-
struct tree_desc t;
469468
unsigned char root[20];
470469

471470
tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
@@ -478,8 +477,13 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
478477
return 0;
479478
}
480479

481-
init_tree_desc(&t, tree, size);
482-
retval = find_tree_entry(&t, name, sha1, mode);
480+
if (!size) {
481+
retval = -1;
482+
} else {
483+
struct tree_desc t;
484+
init_tree_desc(&t, tree, size);
485+
retval = find_tree_entry(&t, name, sha1, mode);
486+
}
483487
free(tree);
484488
return retval;
485489
}

0 commit comments

Comments
 (0)