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

sha1_get_pack_name: use a strbuf

We do some manual memory computation here, and there's no
check that our 60 is not overflowed by the raw sprintf (it
isn't, because the "which" parameter is never longer than
"pack"). We can simplify this greatly with a strbuf.

Technically the end result is not identical, as the original
took care not to rewrite the object directory on each call
for performance reasons.  We could do that here, too (by
saving the baselen and resetting to it), but it's not worth
the complexity; this function is not called a lot (generally
once per packfile that we open).
Signed-off-by: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 54ba4c5f
......@@ -208,44 +208,25 @@ const char *sha1_file_name(const unsigned char *sha1)
* provided by the caller. which should be "pack" or "idx".
*/
static char *sha1_get_pack_name(const unsigned char *sha1,
char **name, char **base, const char *which)
struct strbuf *buf,
const char *which)
{
static const char hex[] = "0123456789abcdef";
char *buf;
int i;
if (!*base) {
const char *sha1_file_directory = get_object_directory();
int len = strlen(sha1_file_directory);
*base = xmalloc(len + 60);
sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
sha1_file_directory, which);
*name = *base + len + 11;
}
buf = *name;
for (i = 0; i < 20; i++) {
unsigned int val = *sha1++;
*buf++ = hex[val >> 4];
*buf++ = hex[val & 0xf];
}
return *base;
strbuf_reset(buf);
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
sha1_to_hex(sha1), which);
return buf->buf;
}
char *sha1_pack_name(const unsigned char *sha1)
{
static char *name, *base;
return sha1_get_pack_name(sha1, &name, &base, "pack");
static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "pack");
}
char *sha1_pack_index_name(const unsigned char *sha1)
{
static char *name, *base;
return sha1_get_pack_name(sha1, &name, &base, "idx");
static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "idx");
}
struct alternate_object_database *alt_odb_list;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册