Commit a693b873 by Patrick Steinhardt

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.
parent 4796c916
...@@ -1267,7 +1267,7 @@ static const char *escaped = "\n\t\b\"\\"; ...@@ -1267,7 +1267,7 @@ static const char *escaped = "\n\t\b\"\\";
/* Escape the values to write them to the file */ /* Escape the values to write them to the file */
static char *escape_value(const char *ptr) static char *escape_value(const char *ptr)
{ {
git_buf buf = GIT_BUF_INIT; git_buf buf;
size_t len; size_t len;
const char *esc; const char *esc;
...@@ -1277,7 +1277,8 @@ static char *escape_value(const char *ptr) ...@@ -1277,7 +1277,8 @@ static char *escape_value(const char *ptr)
if (!len) if (!len)
return git__calloc(1, sizeof(char)); return git__calloc(1, sizeof(char));
git_buf_grow(&buf, len); if (git_buf_init(&buf, len) < 0)
return NULL;
while (*ptr != '\0') { while (*ptr != '\0') {
if ((esc = strchr(escaped, *ptr)) != NULL) { if ((esc = strchr(escaped, *ptr)) != NULL) {
......
...@@ -312,7 +312,7 @@ static int pack_index_open(struct git_pack_file *p) ...@@ -312,7 +312,7 @@ static int pack_index_open(struct git_pack_file *p)
{ {
int error = 0; int error = 0;
size_t name_len; size_t name_len;
git_buf idx_name = GIT_BUF_INIT; git_buf idx_name;
if (p->index_version > -1) if (p->index_version > -1)
return 0; return 0;
...@@ -320,10 +320,13 @@ static int pack_index_open(struct git_pack_file *p) ...@@ -320,10 +320,13 @@ static int pack_index_open(struct git_pack_file *p)
name_len = strlen(p->pack_name); name_len = strlen(p->pack_name);
assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */ 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_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
git_buf_puts(&idx_name, ".idx"); git_buf_puts(&idx_name, ".idx");
if (git_buf_oom(&idx_name)) { if (git_buf_oom(&idx_name)) {
git_buf_free(&idx_name);
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment