Commit 92e0b67930f350a40575954f4638cbc71fee57d2

Vicent Marti 2014-11-21T13:31:30

buffer: Do not `put` anything if len is 0

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);