diff options
author | Patrick Steinhardt <ps@pks.im> | 2017-06-07 09:56:31 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2017-06-08 11:58:22 +0200 |
commit | 4796c916d376af528d8bbf07e8a5e176da6ee928 (patch) | |
tree | 0084d43114a42165f68a8bc6bed09b17aa60d6d9 /src/buffer.c | |
parent | 9a8386a2c649fa74cf90fe95931bc3fc0466f2f6 (diff) | |
download | libgit2-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.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 14 |
1 files changed, 8 insertions, 6 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) |