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>
Miklos Vajna committed
Nov 17, 2008 at 21:48 UTC
d3f9f9a92eca0dd6379d836a083e823cc5e3eeb6
1 file changed
+8
-10
builtin-branch.c
+8
-10
@@ -279,7 +279,7 @@ static int ref_cmp(const void *r1, const void *r2)
279
return strcmp(c1->name, c2->name);
280
}
281
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)
283
{
284
int ours, theirs;
285
struct branch *branch = branch_get(branch_name);
@@ -287,11 +287,11 @@ static void fill_tracking_info(char *stat, const char *branch_name)
287
if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
288
return;
289
if (!ours)
290
- sprintf(stat, "[behind %d] ", theirs);
290
+ strbuf_addf(stat, "[behind %d] ", theirs);
291
else if (!theirs)
292
- sprintf(stat, "[ahead %d] ", ours);
292
+ strbuf_addf(stat, "[ahead %d] ", ours);
293
else
294
- sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
294
+ strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
295
}
296
297
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,
334
}
335
336
if (verbose) {
337
- struct strbuf subject = STRBUF_INIT;
337
+ struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
338
const char *sub = " **** invalid ref ****";
339
- char stat[128];
340
-
341
- stat[0] = '\0';
339
340
commit = item->commit;
341
if (commit && !parse_commit(commit)) {
@@ -348,13 +345,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
345
}
346
347
if (item->kind == REF_LOCAL_BRANCH)
351
- fill_tracking_info(stat, item->name);
348
+ fill_tracking_info(&stat, item->name);
349
350
printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
351
maxwidth, item->name,
352
branch_get_color(COLOR_BRANCH_RESET),
353
find_unique_abbrev(item->commit->object.sha1, abbrev),
357
- stat, sub);
354
+ stat.buf, sub);
355
+ strbuf_release(&stat);
356
strbuf_release(&subject);
357
} else {
358
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,