Skip to content

Commit 2aa9b75

Browse files
committed
Merge branch 'jk/remote-tracking-ref-leakfix' into hn/status-compare-with-push
* jk/remote-tracking-ref-leakfix: remote: always allocate branch.push_tracking_ref remote: fix leak in branch_get_push_1() with invalid "simple" config remote: drop const return of tracking_for_push_dest() remote: return non-const pointer from error_buf()
2 parents 68cb7f9 + d79fff4 commit 2aa9b75

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

remote.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ static void branch_release(struct branch *branch)
272272
free((char *)branch->refname);
273273
free(branch->remote_name);
274274
free(branch->pushremote_name);
275+
free(branch->push_tracking_ref);
275276
merge_clear(branch);
276277
}
277278

@@ -1838,7 +1839,7 @@ int branch_merge_matches(struct branch *branch,
18381839
}
18391840

18401841
__attribute__((format (printf,2,3)))
1841-
static const char *error_buf(struct strbuf *err, const char *fmt, ...)
1842+
static char *error_buf(struct strbuf *err, const char *fmt, ...)
18421843
{
18431844
if (err) {
18441845
va_list ap;
@@ -1876,9 +1877,9 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
18761877
return branch->merge[0]->dst;
18771878
}
18781879

1879-
static const char *tracking_for_push_dest(struct remote *remote,
1880-
const char *refname,
1881-
struct strbuf *err)
1880+
static char *tracking_for_push_dest(struct remote *remote,
1881+
const char *refname,
1882+
struct strbuf *err)
18821883
{
18831884
char *ret;
18841885

@@ -1890,8 +1891,8 @@ static const char *tracking_for_push_dest(struct remote *remote,
18901891
return ret;
18911892
}
18921893

1893-
static const char *branch_get_push_1(struct repository *repo,
1894-
struct branch *branch, struct strbuf *err)
1894+
static char *branch_get_push_1(struct repository *repo,
1895+
struct branch *branch, struct strbuf *err)
18951896
{
18961897
struct remote_state *remote_state = repo->remote_state;
18971898
struct remote *remote;
@@ -1906,7 +1907,7 @@ static const char *branch_get_push_1(struct repository *repo,
19061907

19071908
if (remote->push.nr) {
19081909
char *dst;
1909-
const char *ret;
1910+
char *ret;
19101911

19111912
dst = apply_refspecs(&remote->push, branch->refname);
19121913
if (!dst)
@@ -1931,22 +1932,25 @@ static const char *branch_get_push_1(struct repository *repo,
19311932
return tracking_for_push_dest(remote, branch->refname, err);
19321933

19331934
case PUSH_DEFAULT_UPSTREAM:
1934-
return branch_get_upstream(branch, err);
1935+
return xstrdup_or_null(branch_get_upstream(branch, err));
19351936

19361937
case PUSH_DEFAULT_UNSPECIFIED:
19371938
case PUSH_DEFAULT_SIMPLE:
19381939
{
1939-
const char *up, *cur;
1940+
const char *up;
1941+
char *cur;
19401942

19411943
up = branch_get_upstream(branch, err);
19421944
if (!up)
19431945
return NULL;
19441946
cur = tracking_for_push_dest(remote, branch->refname, err);
19451947
if (!cur)
19461948
return NULL;
1947-
if (strcmp(cur, up))
1949+
if (strcmp(cur, up)) {
1950+
free(cur);
19481951
return error_buf(err,
19491952
_("cannot resolve 'simple' push to a single destination"));
1953+
}
19501954
return cur;
19511955
}
19521956
}

remote.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ struct branch {
331331

332332
int merge_alloc;
333333

334-
const char *push_tracking_ref;
334+
char *push_tracking_ref;
335335
};
336336

337337
struct branch *branch_get(const char *name);

t/for-each-ref-tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,15 @@ test_expect_success ':remotename and :remoteref' '
17441744
)
17451745
'
17461746

1747+
test_expect_success '%(push) with an invalid push-simple config' '
1748+
echo "refs/heads/main " >expect &&
1749+
git -c push.default=simple \
1750+
-c remote.pushdefault=myfork \
1751+
for-each-ref \
1752+
--format="%(refname) %(push)" refs/heads/main >actual &&
1753+
test_cmp expect actual
1754+
'
1755+
17471756
test_expect_success "${git_for_each_ref} --ignore-case ignores case" '
17481757
${git_for_each_ref} --format="%(refname)" refs/heads/MAIN >actual &&
17491758
test_must_be_empty actual &&

0 commit comments

Comments
 (0)