pack: use git_buf when building the index name The way we currently do it depends on the subtlety of strlen vs sizeof and the fact that .pack is one longer than .idx. Let's use a git_buf so we can express the manipulation we want much more clearly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/src/pack.c b/src/pack.c
index 105d675..cd65267 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -319,9 +319,9 @@ static int pack_index_check(const char *path, struct git_pack_file *p)
static int pack_index_open(struct git_pack_file *p)
{
- char *idx_name;
int error = 0;
- size_t name_len, base_len;
+ size_t name_len;
+ git_buf idx_name = GIT_BUF_INIT;
if (p->index_version > -1)
return 0;
@@ -329,22 +329,23 @@ static int pack_index_open(struct git_pack_file *p)
name_len = strlen(p->pack_name);
assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
- if ((idx_name = git__malloc(name_len)) == NULL)
+ git_buf_grow(&idx_name, name_len);
+ git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
+ git_buf_puts(&idx_name, ".idx");
+ if (git_buf_oom(&idx_name)) {
+ giterr_set_oom();
return -1;
-
- base_len = name_len - strlen(".pack");
- memcpy(idx_name, p->pack_name, base_len);
- memcpy(idx_name + base_len, ".idx", sizeof(".idx"));
+ }
if ((error = git_mutex_lock(&p->lock)) < 0) {
- git__free(idx_name);
+ git_buf_free(&idx_name);
return error;
}
if (p->index_version == -1)
- error = pack_index_check(idx_name, p);
+ error = pack_index_check(idx_name.ptr, p);
- git__free(idx_name);
+ git_buf_free(&idx_name);
git_mutex_unlock(&p->lock);