summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2015-02-10 23:55:07 -0500
committerEdward Thomson <ethomson@edwardthomson.com>2015-02-12 22:54:47 -0500
commit4aa664ae3953d99c2ae4cd769f02818bc122eebc (patch)
tree7316e3d7febcbcde19dd4844af9503f4c6d57d69 /src/buffer.c
parent3603cb0978b7ef21ff9cd63693ebd6d27bc2bc53 (diff)
downloadlibgit2-4aa664ae3953d99c2ae4cd769f02818bc122eebc.tar.gz
git_buf_grow_by: increase buf asize incrementally
Introduce `git_buf_grow_by` to incrementally increase the size of a `git_buf`, performing an overflow calculation on the growth.
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/buffer.c b/src/buffer.c
index ee8bba4ec..8098bb1e7 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -101,6 +101,18 @@ int git_buf_grow(git_buf *buffer, size_t target_size)
return git_buf_try_grow(buffer, target_size, true, true);
}
+int git_buf_grow_by(git_buf *buffer, size_t additional_size)
+{
+ if (GIT_ALLOC_OVERFLOW_ADD(buffer->size, additional_size)) {
+ buffer->ptr = git_buf__oom;
+ giterr_set_oom();
+ return -1;
+ }
+
+ return git_buf_try_grow(
+ buffer, buffer->size + additional_size, true, true);
+}
+
void git_buf_free(git_buf *buf)
{
if (!buf) return;
@@ -515,9 +527,8 @@ int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...)
if (total_size == 0)
return 0;
- GITERR_CHECK_ALLOC_ADD(buf->size, total_size);
- GITERR_CHECK_ALLOC_ADD(buf->size + total_size, 1);
- if (git_buf_grow(buf, buf->size + total_size + 1) < 0)
+ GITERR_CHECK_ALLOC_ADD(total_size, 1);
+ if (git_buf_grow_by(buf, total_size + 1) < 0)
return -1;
out = buf->ptr + buf->size;