Skip to contentSkip to conversation

am: check return value of resolve_refdup before using hash

PATCH In review René Scharfe 2 participants 2 messages May 6, 2017 – May 9, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 am: 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.

---
 builtin/am.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/am.c +1 -1
index a95dd8b4e6..2c52c820aa 100644
@@ -2156,7 +2156,7 @@ static void am_abort(struct am_state *state)
 	am_rerere_clear();
 
 	curr_branch = resolve_refdup("HEAD", 0, curr_head.hash, NULL);
-	has_curr_head = !is_null_oid(&curr_head);
+	has_curr_head = curr_branch && !is_null_oid(&curr_head);
 	if (!has_curr_head)
 		hashcpy(curr_head.hash, EMPTY_TREE_SHA1_BIN);
 
Signature · 1 line
2.12.2
#1 lore raw
Quoted 14 lines · René Scharfe

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.

---
 builtin/am.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/am.c +0 -0
index a95dd8b4e6..2c52c820aa 100644

builtin/am.c static void am_abort(struct am_state *state) · line 2161

Quoted 7 lines
builtin/am.c +1 -1
@@ -2156,7 +2156,7 @@ static void am_abort(struct am_state *state)
 	am_rerere_clear();
 
 	curr_branch = resolve_refdup("HEAD", 0, curr_head.hash, NULL);
-	has_curr_head = !is_null_oid(&curr_head);
+	has_curr_head = curr_branch && !is_null_oid(&curr_head);
 	if (!has_curr_head)
 		hashcpy(curr_head.hash, EMPTY_TREE_SHA1_BIN);

This one looks good to me (and no interesting ripple effects from handling the error correctly :) ).

-Peff