threads / patch / 45889

patcham: check return value of resolve_refdup before using hash

Subject: [PATCH] am: check return value of resolve_refdup before using hash

## tl;dr

2 messages between May 6, 2017 and May 9, 2017. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

René Scharfe· May 6, 2017, 17:13 UTC · lore

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.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/am.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to builtin/am.c +1 −1
diff --git a/builtin/am.c b/builtin/am.c
index a95dd8b4e6..2c52c820aa 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -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);
 
-- 
2.12.2
Jeff King· May 9, 2017, 03:58 UTC · re: René Scharfe · lore

Re: [PATCH] am: check return value of resolve_refdup before using hash

On Sat, May 06, 2017 at 07:13:56PM +0200, René Scharfe wrote:
Show 21 quoted lines
> 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.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  builtin/am.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/builtin/am.c b/builtin/am.c
> index a95dd8b4e6..2c52c820aa 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -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

← back to recent threads