summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-06-07 10:20:44 +0200
committerPatrick Steinhardt <ps@pks.im>2017-06-08 11:58:23 +0200
commita693b8734941889bc9a4c31752d317658162246a (patch)
tree1d9a188fb5ae048209759036963e0eff48b93681
parent4796c916d376af528d8bbf07e8a5e176da6ee928 (diff)
downloadlibgit2-a693b8734941889bc9a4c31752d317658162246a.tar.gz
buffer: use `git_buf_init` with length
The `git_buf_init` function has an optional length parameter, which will cause the buffer to be initialized and allocated in one step. This can be used instead of static initialization with `GIT_BUF_INIT` followed by a `git_buf_grow`. This patch does so for two functions where it is applicable.
-rw-r--r--src/config_file.c5
-rw-r--r--src/pack.c7
2 files changed, 8 insertions, 4 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 2302d3343..e15d57bbb 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1267,7 +1267,7 @@ static const char *escaped = "\n\t\b\"\\";
/* Escape the values to write them to the file */
static char *escape_value(const char *ptr)
{
- git_buf buf = GIT_BUF_INIT;
+ git_buf buf;
size_t len;
const char *esc;
@@ -1277,7 +1277,8 @@ static char *escape_value(const char *ptr)
if (!len)
return git__calloc(1, sizeof(char));
- git_buf_grow(&buf, len);
+ if (git_buf_init(&buf, len) < 0)
+ return NULL;
while (*ptr != '\0') {
if ((esc = strchr(escaped, *ptr)) != NULL) {
diff --git a/src/pack.c b/src/pack.c
index d24c13815..f8d0dc9ac 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -312,7 +312,7 @@ static int pack_index_open(struct git_pack_file *p)
{
int error = 0;
size_t name_len;
- git_buf idx_name = GIT_BUF_INIT;
+ git_buf idx_name;
if (p->index_version > -1)
return 0;
@@ -320,10 +320,13 @@ 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 */
- git_buf_grow(&idx_name, name_len);
+ if (git_buf_init(&idx_name, name_len) < 0)
+ return -1;
+
git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
git_buf_puts(&idx_name, ".idx");
if (git_buf_oom(&idx_name)) {
+ git_buf_free(&idx_name);
return -1;
}