提交 bd22d4ff 编写于 作者: J Jeff King 提交者: Junio C Hamano

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: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 6c31c22c
......@@ -528,36 +528,38 @@ static int update_local_ref(struct ref *ref,
}
if (in_merge_bases(current, updated)) {
char quickref[83];
struct strbuf quickref = STRBUF_INIT;
int r;
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
strcat(quickref, "..");
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "..");
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(ref->new_sha1);
r = s_update_ref("fast-forward", ref, 1);
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
r ? '!' : ' ',
TRANSPORT_SUMMARY_WIDTH, quickref,
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
REFCOL_WIDTH, remote, pretty_ref,
r ? _(" (unable to update local ref)") : "");
strbuf_release(&quickref);
return r;
} else if (force || ref->force) {
char quickref[84];
struct strbuf quickref = STRBUF_INIT;
int r;
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
strcat(quickref, "...");
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "...");
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(ref->new_sha1);
r = s_update_ref("forced-update", ref, 1);
strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
r ? '!' : '+',
TRANSPORT_SUMMARY_WIDTH, quickref,
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
REFCOL_WIDTH, remote, pretty_ref,
r ? _("unable to update local ref") : _("forced update"));
strbuf_release(&quickref);
return r;
} else {
strbuf_addf(display, "! %-*s %-*s -> %s %s",
......
......@@ -654,23 +654,24 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
"[new branch]"),
ref, ref->peer_ref, NULL, porcelain);
else {
char quickref[84];
struct strbuf quickref = STRBUF_INIT;
char type;
const char *msg;
strcpy(quickref, status_abbrev(ref->old_sha1));
strbuf_addstr(&quickref, status_abbrev(ref->old_sha1));
if (ref->forced_update) {
strcat(quickref, "...");
strbuf_addstr(&quickref, "...");
type = '+';
msg = "forced update";
} else {
strcat(quickref, "..");
strbuf_addstr(&quickref, "..");
type = ' ';
msg = NULL;
}
strcat(quickref, status_abbrev(ref->new_sha1));
strbuf_addstr(&quickref, status_abbrev(ref->new_sha1));
print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
strbuf_release(&quickref);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册