Skip to content

Commit 79e913c

Browse files
rscharfegitster
authored andcommitted
checkout: check return value of resolve_refdup before using hash
If resolve_refdup() fails it returns NULL and possibly leaves its hash output parameter untouched. Make sure to use it only if the function succeeded, in order to avoid accessing uninitialized memory. Found with t/t2011-checkout-invalid-head.sh --valgrind. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49800c9 commit 79e913c

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

builtin/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ static int switch_branches(const struct checkout_opts *opts,
814814
int flag, writeout_error = 0;
815815
memset(&old, 0, sizeof(old));
816816
old.path = path_to_free = resolve_refdup("HEAD", 0, rev.hash, &flag);
817-
old.commit = lookup_commit_reference_gently(rev.hash, 1);
817+
if (old.path)
818+
old.commit = lookup_commit_reference_gently(rev.hash, 1);
818819
if (!(flag & REF_ISSYMREF))
819820
old.path = NULL;
820821

0 commit comments

Comments
 (0)