1
0
mirror of https://github.com/git/git.git synced 2024-10-20 22:48:24 +02:00

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: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2015-09-24 17:07:34 -04:00 committed by Junio C Hamano
parent 54ba4c5fa2
commit ac5190cc48

@ -208,44 +208,25 @@ const char *sha1_file_name(const unsigned char *sha1)
* provided by the caller. which should be "pack" or "idx". * provided by the caller. which should be "pack" or "idx".
*/ */
static char *sha1_get_pack_name(const unsigned char *sha1, 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"; strbuf_reset(buf);
char *buf; strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
int i; sha1_to_hex(sha1), which);
return buf->buf;
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;
} }
char *sha1_pack_name(const unsigned char *sha1) char *sha1_pack_name(const unsigned char *sha1)
{ {
static char *name, *base; static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "pack");
return sha1_get_pack_name(sha1, &name, &base, "pack");
} }
char *sha1_pack_index_name(const unsigned char *sha1) char *sha1_pack_index_name(const unsigned char *sha1)
{ {
static char *name, *base; static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "idx");
return sha1_get_pack_name(sha1, &name, &base, "idx");
} }
struct alternate_object_database *alt_odb_list; struct alternate_object_database *alt_odb_list;