Skip to content

Commit a693b87

Browse files
committed
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.
1 parent 4796c91 commit a693b87

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/config_file.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ static const char *escaped = "\n\t\b\"\\";
12671267
/* Escape the values to write them to the file */
12681268
static char *escape_value(const char *ptr)
12691269
{
1270-
git_buf buf = GIT_BUF_INIT;
1270+
git_buf buf;
12711271
size_t len;
12721272
const char *esc;
12731273

@@ -1277,7 +1277,8 @@ static char *escape_value(const char *ptr)
12771277
if (!len)
12781278
return git__calloc(1, sizeof(char));
12791279

1280-
git_buf_grow(&buf, len);
1280+
if (git_buf_init(&buf, len) < 0)
1281+
return NULL;
12811282

12821283
while (*ptr != '\0') {
12831284
if ((esc = strchr(escaped, *ptr)) != NULL) {

src/pack.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,21 @@ static int pack_index_open(struct git_pack_file *p)
312312
{
313313
int error = 0;
314314
size_t name_len;
315-
git_buf idx_name = GIT_BUF_INIT;
315+
git_buf idx_name;
316316

317317
if (p->index_version > -1)
318318
return 0;
319319

320320
name_len = strlen(p->pack_name);
321321
assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
322322

323-
git_buf_grow(&idx_name, name_len);
323+
if (git_buf_init(&idx_name, name_len) < 0)
324+
return -1;
325+
324326
git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
325327
git_buf_puts(&idx_name, ".idx");
326328
if (git_buf_oom(&idx_name)) {
329+
git_buf_free(&idx_name);
327330
return -1;
328331
}
329332

0 commit comments

Comments
 (0)