summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-06-07 09:56:31 +0200
committerPatrick Steinhardt <ps@pks.im>2017-06-08 11:58:22 +0200
commit4796c916d376af528d8bbf07e8a5e176da6ee928 (patch)
tree0084d43114a42165f68a8bc6bed09b17aa60d6d9
parent9a8386a2c649fa74cf90fe95931bc3fc0466f2f6 (diff)
downloadlibgit2-4796c916d376af528d8bbf07e8a5e176da6ee928.tar.gz
buffer: return errors for `git_buf_init` and `git_buf_attach`
Both the `git_buf_init` and `git_buf_attach` functions may call `git_buf_grow` in case they were given an allocation length as parameter. As such, it is possible for these functions to fail when we run out of memory. While it won't probably be used anytime soon, it does indeed make sense to also record this fact by returning an error code from both functions. As they belong to the internal API only, this change does not break our interface.
-rw-r--r--src/buffer.c14
-rw-r--r--src/buffer.h4
2 files changed, 10 insertions, 8 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 40bed5c98..6dfcbfbe6 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -22,14 +22,15 @@ char git_buf__oom[1];
return -1;
-void git_buf_init(git_buf *buf, size_t initial_size)
+int git_buf_init(git_buf *buf, size_t initial_size)
{
buf->asize = 0;
buf->size = 0;
buf->ptr = git_buf__initbuf;
- if (initial_size)
- git_buf_grow(buf, initial_size);
+ ENSURE_SIZE(buf, initial_size);
+
+ return 0;
}
int git_buf_try_grow(
@@ -577,7 +578,7 @@ char *git_buf_detach(git_buf *buf)
return data;
}
-void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
+int git_buf_attach(git_buf *buf, char *ptr, size_t asize)
{
git_buf_free(buf);
@@ -588,9 +589,10 @@ void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
else /* pass 0 to fall back on strlen + 1 */
buf->asize = buf->size + 1;
- } else {
- git_buf_grow(buf, asize);
}
+
+ ENSURE_SIZE(buf, asize);
+ return 0;
}
void git_buf_attach_notowned(git_buf *buf, const char *ptr, size_t size)
diff --git a/src/buffer.h b/src/buffer.h
index a76b2d771..b0aece488 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -34,7 +34,7 @@ GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
* For the cases where GIT_BUF_INIT cannot be used to do static
* initialization.
*/
-extern void git_buf_init(git_buf *buf, size_t initial_size);
+extern int git_buf_init(git_buf *buf, size_t initial_size);
/**
* Resize the buffer allocation to make more space.
@@ -73,7 +73,7 @@ extern void git_buf_sanitize(git_buf *buf);
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
extern char *git_buf_detach(git_buf *buf);
-extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
+extern int git_buf_attach(git_buf *buf, char *ptr, size_t asize);
/* Populates a `git_buf` where the contents are not "owned" by the
* buffer, and calls to `git_buf_free` will not free the given buf.