diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-25 15:26:43 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-25 15:26:43 -0400 |
| commit | 87987fd1e0f00a85c5fa16a96ca52decede629ae (patch) | |
| tree | f6c7031ea2e936a5c73dd247d038d43a1b57780b /src | |
| parent | daacf96d101b9d2100a5028090b5af5249d8893d (diff) | |
| parent | 3cf91d98e28f2519e69c9ce77a6d6454adc713d7 (diff) | |
| download | libgit2-87987fd1e0f00a85c5fa16a96ca52decede629ae.tar.gz | |
Merge pull request #3246 from libgit2/cmn/dont-grow-borrowed
Don't allow growing borrowed buffers
Diffstat (limited to 'src')
| -rw-r--r-- | src/buffer.c | 14 | ||||
| -rw-r--r-- | src/buffer.h | 2 | ||||
| -rw-r--r-- | src/path.c | 4 |
3 files changed, 11 insertions, 9 deletions
diff --git a/src/buffer.c b/src/buffer.c index f633c5e02..1a5809cca 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -33,7 +33,7 @@ void git_buf_init(git_buf *buf, size_t initial_size) } int git_buf_try_grow( - git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external) + git_buf *buf, size_t target_size, bool mark_oom) { char *new_ptr; size_t new_size; @@ -41,6 +41,11 @@ int git_buf_try_grow( if (buf->ptr == git_buf__oom) return -1; + if (buf->asize == 0 && buf->size != 0) { + giterr_set(GITERR_INVALID, "cannot grow a borrowed buffer"); + return GIT_EINVALID; + } + if (!target_size) target_size = buf->size; @@ -82,9 +87,6 @@ int git_buf_try_grow( return -1; } - if (preserve_external && !buf->asize && buf->ptr != NULL && buf->size > 0) - memcpy(new_ptr, buf->ptr, min(buf->size, new_size)); - buf->asize = new_size; buf->ptr = new_ptr; @@ -98,7 +100,7 @@ int git_buf_try_grow( int git_buf_grow(git_buf *buffer, size_t target_size) { - return git_buf_try_grow(buffer, target_size, true, true); + return git_buf_try_grow(buffer, target_size, true); } int git_buf_grow_by(git_buf *buffer, size_t additional_size) @@ -110,7 +112,7 @@ int git_buf_grow_by(git_buf *buffer, size_t additional_size) return -1; } - return git_buf_try_grow(buffer, newsize, true, true); + return git_buf_try_grow(buffer, newsize, true); } void git_buf_free(git_buf *buf) diff --git a/src/buffer.h b/src/buffer.h index 093ed9b60..e46ee5dd7 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -59,7 +59,7 @@ extern int git_buf_grow_by(git_buf *buffer, size_t additional_size); * into the newly allocated buffer. */ extern int git_buf_try_grow( - git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external); + git_buf *buf, size_t target_size, bool mark_oom); /** * Sanitizes git_buf structures provided from user input. Users of the diff --git a/src/path.c b/src/path.c index c2c90e48d..2558058dd 100644 --- a/src/path.c +++ b/src/path.c @@ -640,7 +640,7 @@ static bool _check_dir_contents( /* leave base valid even if we could not make space for subdir */ if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) || GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) || - git_buf_try_grow(dir, alloc_size, false, false) < 0) + git_buf_try_grow(dir, alloc_size, false) < 0) return false; /* save excursion */ @@ -847,7 +847,7 @@ int git_path_make_relative(git_buf *path, const char *parent) /* save the offset as we might realllocate the pointer */ offset = p - path->ptr; - if (git_buf_try_grow(path, alloclen, 1, 0) < 0) + if (git_buf_try_grow(path, alloclen, 1) < 0) return -1; p = path->ptr + offset; |
