buffer: don't allow growing borrowed buffers When we don't own a buffer (asize=0) we currently allow the usage of grow to copy the memory into a buffer we do own. This muddles the meaning of grow, and lets us be a bit cavalier with ownership semantics. Don't allow this any more. Usage of grow should be restricted to buffers which we know own their own memory. If unsure, we must not attempt to modify it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
diff --git a/src/buffer.c b/src/buffer.c
index f633c5e..c066d8e 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,9 @@ int git_buf_try_grow(
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;
@@ -82,9 +85,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 +98,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 +110,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 093ed9b..e46ee5d 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 c2c90e4..2558058 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;
diff --git a/tests/core/buffer.c b/tests/core/buffer.c
index fef37f8..cc2d7bb 100644
--- a/tests/core/buffer.c
+++ b/tests/core/buffer.c
@@ -1153,3 +1153,16 @@ void test_core_buffer__lf_and_crlf_conversions(void)
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));
+}