Skip to content

Commit 458cea5

Browse files
authored
Merge pull request libgit2#4255 from pks-t/pks/buffer-grow-errors
Buffer growing cleanups
2 parents 90500d8 + a693b87 commit 458cea5

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/buffer.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ char git_buf__initbuf[1];
1818
char git_buf__oom[1];
1919

2020
#define ENSURE_SIZE(b, d) \
21-
if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\
21+
if ((d) > (b)->asize && git_buf_grow((b), (d)) < 0)\
2222
return -1;
2323

2424

25-
void git_buf_init(git_buf *buf, size_t initial_size)
25+
int git_buf_init(git_buf *buf, size_t initial_size)
2626
{
2727
buf->asize = 0;
2828
buf->size = 0;
2929
buf->ptr = git_buf__initbuf;
3030

31-
if (initial_size)
32-
git_buf_grow(buf, initial_size);
31+
ENSURE_SIZE(buf, initial_size);
32+
33+
return 0;
3334
}
3435

3536
int git_buf_try_grow(
@@ -577,7 +578,7 @@ char *git_buf_detach(git_buf *buf)
577578
return data;
578579
}
579580

580-
void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
581+
int git_buf_attach(git_buf *buf, char *ptr, size_t asize)
581582
{
582583
git_buf_free(buf);
583584

@@ -588,9 +589,10 @@ void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
588589
buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
589590
else /* pass 0 to fall back on strlen + 1 */
590591
buf->asize = buf->size + 1;
591-
} else {
592-
git_buf_grow(buf, asize);
593592
}
593+
594+
ENSURE_SIZE(buf, asize);
595+
return 0;
594596
}
595597

596598
void git_buf_attach_notowned(git_buf *buf, const char *ptr, size_t size)
@@ -724,9 +726,7 @@ int git_buf_join(
724726
GITERR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
725727
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep);
726728
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
727-
if (git_buf_grow(buf, alloc_len) < 0)
728-
return -1;
729-
assert(buf->ptr);
729+
ENSURE_SIZE(buf, alloc_len);
730730

731731
/* fix up internal pointers */
732732
if (offset_a >= 0)
@@ -780,8 +780,7 @@ int git_buf_join3(
780780
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b);
781781
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, len_c);
782782
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, 1);
783-
if (git_buf_grow(buf, len_total) < 0)
784-
return -1;
783+
ENSURE_SIZE(buf, len_total);
785784

786785
tgt = buf->ptr;
787786

src/buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
3434
* For the cases where GIT_BUF_INIT cannot be used to do static
3535
* initialization.
3636
*/
37-
extern void git_buf_init(git_buf *buf, size_t initial_size);
37+
extern int git_buf_init(git_buf *buf, size_t initial_size);
3838

3939
/**
4040
* Resize the buffer allocation to make more space.
@@ -73,7 +73,7 @@ extern void git_buf_sanitize(git_buf *buf);
7373

7474
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
7575
extern char *git_buf_detach(git_buf *buf);
76-
extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
76+
extern int git_buf_attach(git_buf *buf, char *ptr, size_t asize);
7777

7878
/* Populates a `git_buf` where the contents are not "owned" by the
7979
* buffer, and calls to `git_buf_free` will not free the given buf.

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +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)) {
327-
giterr_set_oom();
329+
git_buf_free(&idx_name);
328330
return -1;
329331
}
330332

src/transports/winhttp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ static int winhttp_stream_connect(winhttp_stream *s)
429429
git_buf_printf(&processed_url, ":%s", t->proxy_connection_data.port);
430430

431431
if (git_buf_oom(&processed_url)) {
432-
giterr_set_oom();
433432
error = -1;
434433
goto on_error;
435434
}

0 commit comments

Comments
 (0)