Skip to content

Commit 2c48420

Browse files
drafnelgitster
authored andcommitted
builtin/checkout.c: don't leak memory in check_tracking_name
remote_find_tracking() populates the query struct with an allocated string in the dst member. So, we do not need to xstrdup() the string, since we can transfer ownership from the query struct (which will go out of scope at the end of this function) to our callback struct, but we must free the string if it will not be used so we will not leak memory. Let's do so. Signed-off-by: Brandon Casey <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 77eb44b commit 2c48420

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

builtin/checkout.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,13 +838,16 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
838838
memset(&query, 0, sizeof(struct refspec));
839839
query.src = cb->src_ref;
840840
if (remote_find_tracking(remote, &query) ||
841-
get_sha1(query.dst, cb->dst_sha1))
841+
get_sha1(query.dst, cb->dst_sha1)) {
842+
free(query.dst);
842843
return 0;
844+
}
843845
if (cb->dst_ref) {
846+
free(query.dst);
844847
cb->unique = 0;
845848
return 0;
846849
}
847-
cb->dst_ref = xstrdup(query.dst);
850+
cb->dst_ref = query.dst;
848851
return 0;
849852
}
850853

0 commit comments

Comments
 (0)