buffer: Do not `put` anything if len is 0
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
diff --git a/src/buffer.c b/src/buffer.c
index e9c420e..7744d8f 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -176,10 +176,13 @@ int git_buf_putcn(git_buf *buf, char c, size_t len)
int git_buf_put(git_buf *buf, const char *data, size_t len)
{
- ENSURE_SIZE(buf, buf->size + len + 1);
- memmove(buf->ptr + buf->size, data, len);
- buf->size += len;
- buf->ptr[buf->size] = '\0';
+ if (len) {
+ assert(data);
+ ENSURE_SIZE(buf, buf->size + len + 1);
+ memmove(buf->ptr + buf->size, data, len);
+ buf->size += len;
+ buf->ptr[buf->size] = '\0';
+ }
return 0;
}
diff --git a/tests/core/buffer.c b/tests/core/buffer.c
index 641fed6..87dec46 100644
--- a/tests/core/buffer.c
+++ b/tests/core/buffer.c
@@ -281,11 +281,10 @@ check_buf_append_abc(
/* more variations on append tests */
void test_core_buffer__5(void)
{
- check_buf_append("", "", "", 0, 8);
- check_buf_append("a", "", "a", 1, 8);
+ check_buf_append("", "", "", 0, 0);
+ check_buf_append("a", "", "a", 1, 0);
check_buf_append("", "a", "a", 1, 8);
check_buf_append("", "a", "a", 1, 8);
- check_buf_append("a", "", "a", 1, 8);
check_buf_append("a", "b", "ab", 2, 8);
check_buf_append("", "abcdefgh", "abcdefgh", 8, 16);
check_buf_append("abcdefgh", "", "abcdefgh", 8, 16);