Skip to content

Commit d3f9f9a

Browse files
Miklos Vajnagitster
authored andcommitted
builtin-branch: use strbuf in fill_tracking_info()
This is just about using the API, though in case of ~ 10^100 commits, this would fix the problem of writing to unallocated memory as well. ;-) Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3c59c50 commit d3f9f9a

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

builtin-branch.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,19 @@ static int ref_cmp(const void *r1, const void *r2)
279279
return strcmp(c1->name, c2->name);
280280
}
281281

282-
static void fill_tracking_info(char *stat, const char *branch_name)
282+
static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
283283
{
284284
int ours, theirs;
285285
struct branch *branch = branch_get(branch_name);
286286

287287
if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
288288
return;
289289
if (!ours)
290-
sprintf(stat, "[behind %d] ", theirs);
290+
strbuf_addf(stat, "[behind %d] ", theirs);
291291
else if (!theirs)
292-
sprintf(stat, "[ahead %d] ", ours);
292+
strbuf_addf(stat, "[ahead %d] ", ours);
293293
else
294-
sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
294+
strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
295295
}
296296

297297
static int matches_merge_filter(struct commit *commit)
@@ -334,11 +334,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
334334
}
335335

336336
if (verbose) {
337-
struct strbuf subject = STRBUF_INIT;
337+
struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
338338
const char *sub = " **** invalid ref ****";
339-
char stat[128];
340-
341-
stat[0] = '\0';
342339

343340
commit = item->commit;
344341
if (commit && !parse_commit(commit)) {
@@ -348,13 +345,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
348345
}
349346

350347
if (item->kind == REF_LOCAL_BRANCH)
351-
fill_tracking_info(stat, item->name);
348+
fill_tracking_info(&stat, item->name);
352349

353350
printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
354351
maxwidth, item->name,
355352
branch_get_color(COLOR_BRANCH_RESET),
356353
find_unique_abbrev(item->commit->object.sha1, abbrev),
357-
stat, sub);
354+
stat.buf, sub);
355+
strbuf_release(&stat);
358356
strbuf_release(&subject);
359357
} else {
360358
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,

0 commit comments

Comments
 (0)