Skip to content

Commit 21de7e9

Browse files
committed
Merge branch 'rs/refspec-leakfix'
Leakfix. * rs/refspec-leakfix: refspec: add and use refspec_appendf() push: release strbufs used for refspec formatting
2 parents 9b80744 + 1af8b8c commit 21de7e9

File tree

6 files changed

+47
-51
lines changed

6 files changed

+47
-51
lines changed

builtin/clone.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
953953
struct ref *mapped_refs;
954954
const struct ref *ref;
955955
struct strbuf key = STRBUF_INIT;
956-
struct strbuf default_refspec = STRBUF_INIT;
957956
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
958957
struct transport *transport = NULL;
959958
const char *src_ref_prefix = "refs/heads/";
@@ -1157,9 +1156,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11571156

11581157
remote = remote_get(option_origin);
11591158

1160-
strbuf_addf(&default_refspec, "+%s*:%s*", src_ref_prefix,
1161-
branch_top.buf);
1162-
refspec_append(&remote->fetch, default_refspec.buf);
1159+
refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1160+
branch_top.buf);
11631161

11641162
transport = transport_get(remote, remote->url[0]);
11651163
transport_set_verbosity(transport, option_verbosity, option_progress);
@@ -1332,7 +1330,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13321330
strbuf_release(&reflog_msg);
13331331
strbuf_release(&branch_top);
13341332
strbuf_release(&key);
1335-
strbuf_release(&default_refspec);
13361333
junk_mode = JUNK_LEAVE_ALL;
13371334

13381335
strvec_clear(&ref_prefixes);

builtin/fetch.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,15 +1738,12 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
17381738

17391739
for (i = 0; i < argc; i++) {
17401740
if (!strcmp(argv[i], "tag")) {
1741-
char *tag;
17421741
i++;
17431742
if (i >= argc)
17441743
die(_("You need to specify a tag name."));
17451744

1746-
tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1747-
argv[i], argv[i]);
1748-
refspec_append(&rs, tag);
1749-
free(tag);
1745+
refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
1746+
argv[i], argv[i]);
17501747
} else {
17511748
refspec_append(&rs, argv[i]);
17521749
}

builtin/push.c

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,41 @@ static struct refspec rs = REFSPEC_INIT_PUSH;
6161

6262
static struct string_list push_options_config = STRING_LIST_INIT_DUP;
6363

64-
static const char *map_refspec(const char *ref,
65-
struct remote *remote, struct ref *local_refs)
64+
static void refspec_append_mapped(struct refspec *refspec, const char *ref,
65+
struct remote *remote, struct ref *local_refs)
6666
{
6767
const char *branch_name;
6868
struct ref *matched = NULL;
6969

7070
/* Does "ref" uniquely name our ref? */
71-
if (count_refspec_match(ref, local_refs, &matched) != 1)
72-
return ref;
71+
if (count_refspec_match(ref, local_refs, &matched) != 1) {
72+
refspec_append(refspec, ref);
73+
return;
74+
}
7375

7476
if (remote->push.nr) {
7577
struct refspec_item query;
7678
memset(&query, 0, sizeof(struct refspec_item));
7779
query.src = matched->name;
7880
if (!query_refspecs(&remote->push, &query) && query.dst) {
79-
struct strbuf buf = STRBUF_INIT;
80-
strbuf_addf(&buf, "%s%s:%s",
81-
query.force ? "+" : "",
82-
query.src, query.dst);
83-
return strbuf_detach(&buf, NULL);
81+
refspec_appendf(refspec, "%s%s:%s",
82+
query.force ? "+" : "",
83+
query.src, query.dst);
84+
return;
8485
}
8586
}
8687

8788
if (push_default == PUSH_DEFAULT_UPSTREAM &&
8889
skip_prefix(matched->name, "refs/heads/", &branch_name)) {
8990
struct branch *branch = branch_get(branch_name);
9091
if (branch->merge_nr == 1 && branch->merge[0]->src) {
91-
struct strbuf buf = STRBUF_INIT;
92-
strbuf_addf(&buf, "%s:%s",
93-
ref, branch->merge[0]->src);
94-
return strbuf_detach(&buf, NULL);
92+
refspec_appendf(refspec, "%s:%s",
93+
ref, branch->merge[0]->src);
94+
return;
9595
}
9696
}
9797

98-
return ref;
98+
refspec_append(refspec, ref);
9999
}
100100

101101
static void set_refspecs(const char **refs, int nr, const char *repo)
@@ -107,30 +107,26 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
107107
for (i = 0; i < nr; i++) {
108108
const char *ref = refs[i];
109109
if (!strcmp("tag", ref)) {
110-
struct strbuf tagref = STRBUF_INIT;
111110
if (nr <= ++i)
112111
die(_("tag shorthand without <tag>"));
113112
ref = refs[i];
114113
if (deleterefs)
115-
strbuf_addf(&tagref, ":refs/tags/%s", ref);
114+
refspec_appendf(&rs, ":refs/tags/%s", ref);
116115
else
117-
strbuf_addf(&tagref, "refs/tags/%s", ref);
118-
ref = strbuf_detach(&tagref, NULL);
116+
refspec_appendf(&rs, "refs/tags/%s", ref);
119117
} else if (deleterefs) {
120-
struct strbuf delref = STRBUF_INIT;
121118
if (strchr(ref, ':'))
122119
die(_("--delete only accepts plain target ref names"));
123-
strbuf_addf(&delref, ":%s", ref);
124-
ref = strbuf_detach(&delref, NULL);
120+
refspec_appendf(&rs, ":%s", ref);
125121
} else if (!strchr(ref, ':')) {
126122
if (!remote) {
127123
/* lazily grab remote and local_refs */
128124
remote = remote_get(repo);
129125
local_refs = get_local_heads();
130126
}
131-
ref = map_refspec(ref, remote, local_refs);
132-
}
133-
refspec_append(&rs, ref);
127+
refspec_append_mapped(&rs, ref, remote, local_refs);
128+
} else
129+
refspec_append(&rs, ref);
134130
}
135131
}
136132

@@ -192,8 +188,6 @@ static const char message_detached_head_die[] =
192188
static void setup_push_upstream(struct remote *remote, struct branch *branch,
193189
int triangular, int simple)
194190
{
195-
struct strbuf refspec = STRBUF_INIT;
196-
197191
if (!branch)
198192
die(_(message_detached_head_die), remote->name);
199193
if (!branch->merge_nr || !branch->merge || !branch->remote_name)
@@ -219,18 +213,14 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch,
219213
die_push_simple(branch, remote);
220214
}
221215

222-
strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
223-
refspec_append(&rs, refspec.buf);
216+
refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
224217
}
225218

226219
static void setup_push_current(struct remote *remote, struct branch *branch)
227220
{
228-
struct strbuf refspec = STRBUF_INIT;
229-
230221
if (!branch)
231222
die(_(message_detached_head_die), remote->name);
232-
strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
233-
refspec_append(&rs, refspec.buf);
223+
refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
234224
}
235225

236226
static int is_workflow_triangular(struct remote *remote)

refspec.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void refspec_init(struct refspec *rs, int fetch)
153153
rs->fetch = fetch;
154154
}
155155

156-
void refspec_append(struct refspec *rs, const char *refspec)
156+
static void refspec_append_nodup(struct refspec *rs, char *refspec)
157157
{
158158
struct refspec_item item;
159159

@@ -163,7 +163,21 @@ void refspec_append(struct refspec *rs, const char *refspec)
163163
rs->items[rs->nr++] = item;
164164

165165
ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
166-
rs->raw[rs->raw_nr++] = xstrdup(refspec);
166+
rs->raw[rs->raw_nr++] = refspec;
167+
}
168+
169+
void refspec_append(struct refspec *rs, const char *refspec)
170+
{
171+
refspec_append_nodup(rs, xstrdup(refspec));
172+
}
173+
174+
void refspec_appendf(struct refspec *rs, const char *fmt, ...)
175+
{
176+
va_list ap;
177+
178+
va_start(ap, fmt);
179+
refspec_append_nodup(rs, xstrvfmt(fmt, ap));
180+
va_end(ap);
167181
}
168182

169183
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)

refspec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
5656
void refspec_item_clear(struct refspec_item *item);
5757
void refspec_init(struct refspec *rs, int fetch);
5858
void refspec_append(struct refspec *rs, const char *refspec);
59+
__attribute__((format (printf,2,3)))
60+
void refspec_appendf(struct refspec *rs, const char *fmt, ...);
5961
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
6062
void refspec_clear(struct refspec *rs);
6163

remote.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,15 @@ static void read_branches_file(struct remote *remote)
287287
frag = (char *)git_default_branch_name();
288288

289289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290-
strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
291-
frag, remote->name);
292-
refspec_append(&remote->fetch, buf.buf);
290+
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
291+
frag, remote->name);
293292

294293
/*
295294
* Cogito compatible push: push current HEAD to remote #branch
296295
* (master if missing)
297296
*/
298-
strbuf_reset(&buf);
299-
strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
300-
refspec_append(&remote->push, buf.buf);
297+
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
301298
remote->fetch_tags = 1; /* always auto-follow */
302-
strbuf_release(&buf);
303299
}
304300

305301
static int handle_config(const char *key, const char *value, void *cb)

0 commit comments

Comments
 (0)