Skip to content

Commit bd22d4f

Browse files
peffgitster
authored andcommitted
transport: use strbufs for status table "quickref" strings
We generate range strings like "1234abcd...5678efab" for use in the the fetch and push status tables. We use fixed-size buffers along with strcat to do so. These aren't buggy, as our manual size computation is correct, but there's nothing checking that this is so. Let's switch them to strbufs instead, which are obviously correct, and make it easier to audit the code base for problematic calls to strcat(). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c31c22 commit bd22d4f

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

builtin/fetch.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -528,36 +528,38 @@ static int update_local_ref(struct ref *ref,
528528
}
529529

530530
if (in_merge_bases(current, updated)) {
531-
char quickref[83];
531+
struct strbuf quickref = STRBUF_INIT;
532532
int r;
533-
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
534-
strcat(quickref, "..");
535-
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
533+
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
534+
strbuf_addstr(&quickref, "..");
535+
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
536536
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
537537
(recurse_submodules != RECURSE_SUBMODULES_ON))
538538
check_for_new_submodule_commits(ref->new_sha1);
539539
r = s_update_ref("fast-forward", ref, 1);
540540
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
541541
r ? '!' : ' ',
542-
TRANSPORT_SUMMARY_WIDTH, quickref,
542+
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
543543
REFCOL_WIDTH, remote, pretty_ref,
544544
r ? _(" (unable to update local ref)") : "");
545+
strbuf_release(&quickref);
545546
return r;
546547
} else if (force || ref->force) {
547-
char quickref[84];
548+
struct strbuf quickref = STRBUF_INIT;
548549
int r;
549-
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
550-
strcat(quickref, "...");
551-
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
550+
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
551+
strbuf_addstr(&quickref, "...");
552+
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
552553
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
553554
(recurse_submodules != RECURSE_SUBMODULES_ON))
554555
check_for_new_submodule_commits(ref->new_sha1);
555556
r = s_update_ref("forced-update", ref, 1);
556557
strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
557558
r ? '!' : '+',
558-
TRANSPORT_SUMMARY_WIDTH, quickref,
559+
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
559560
REFCOL_WIDTH, remote, pretty_ref,
560561
r ? _("unable to update local ref") : _("forced update"));
562+
strbuf_release(&quickref);
561563
return r;
562564
} else {
563565
strbuf_addf(display, "! %-*s %-*s -> %s %s",

transport.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,23 +654,24 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
654654
"[new branch]"),
655655
ref, ref->peer_ref, NULL, porcelain);
656656
else {
657-
char quickref[84];
657+
struct strbuf quickref = STRBUF_INIT;
658658
char type;
659659
const char *msg;
660660

661-
strcpy(quickref, status_abbrev(ref->old_sha1));
661+
strbuf_addstr(&quickref, status_abbrev(ref->old_sha1));
662662
if (ref->forced_update) {
663-
strcat(quickref, "...");
663+
strbuf_addstr(&quickref, "...");
664664
type = '+';
665665
msg = "forced update";
666666
} else {
667-
strcat(quickref, "..");
667+
strbuf_addstr(&quickref, "..");
668668
type = ' ';
669669
msg = NULL;
670670
}
671-
strcat(quickref, status_abbrev(ref->new_sha1));
671+
strbuf_addstr(&quickref, status_abbrev(ref->new_sha1));
672672

673-
print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
673+
print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
674+
strbuf_release(&quickref);
674675
}
675676
}
676677

0 commit comments

Comments
 (0)