diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2015-06-10 10:44:14 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2015-06-10 10:44:14 +0200 |
commit | 878293f7e1d62b951aaa7dc106ca9c6783112d96 (patch) | |
tree | 8bf2c0c3812b7ce64abf8b6fb0458f37e9defd92 | |
parent | ca2857d81b5783b03d32ddc9d6f338bfec8d93dd (diff) | |
download | libgit2-878293f7e1d62b951aaa7dc106ca9c6783112d96.tar.gz |
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.
-rw-r--r-- | src/pack.c | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/pack.c b/src/pack.c index 105d67510..cd6526721 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); |