}
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;
if (buf->ptr == git_buf__oom)
return -1;
+ if (buf->asize == 0 && buf->size != 0)
+ return GIT_EINVALIDSPEC;
+
if (!target_size)
target_size = buf->size;
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;
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)
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)
* 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
/* 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 */
/* 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;
git_buf_free(&src);
git_buf_free(&tgt);
}
+
+void test_core_buffer__dont_grow_borrowed(void)
+{
+ const char *somestring = "blah blah";
+ git_buf buf = GIT_BUF_INIT;
+
+ git_buf_attach_notowned(&buf, somestring, strlen(somestring) + 1);
+ cl_assert_equal_p(somestring, buf.ptr);
+ cl_assert_equal_i(0, buf.asize);
+ cl_assert_equal_i(strlen(somestring) + 1, buf.size);
+
+ cl_git_fail_with(GIT_EINVALIDSPEC, git_buf_grow(&buf, 1024));
+}