Skip to content

Commit 9a8386a

Browse files
committed
buffer: consistently use ENSURE_SIZE to grow buffers on-demand
The `ENSURE_SIZE` macro can be used to grow a buffer if its currently allocated size does not suffice a required target size. While most of the code already uses this macro, the `git_buf_join` and `git_buf_join3` functions do not yet use it. Due to the macro first checking whether we have to grow the buffer at all, this has the benefit of saving a function call when it is not needed. While this is nice to have, it will probably not matter at all performance-wise -- instead, this only serves for consistency across the code.
1 parent e82dd81 commit 9a8386a

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/buffer.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,7 @@ int git_buf_join(
724724
GITERR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
725725
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep);
726726
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);
727+
ENSURE_SIZE(buf, alloc_len);
730728

731729
/* fix up internal pointers */
732730
if (offset_a >= 0)
@@ -780,8 +778,7 @@ int git_buf_join3(
780778
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b);
781779
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, len_c);
782780
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, 1);
783-
if (git_buf_grow(buf, len_total) < 0)
784-
return -1;
781+
ENSURE_SIZE(buf, len_total);
785782

786783
tgt = buf->ptr;
787784

0 commit comments

Comments
 (0)