Commit 6adcaab70cdd6ced307a71945a1f80833ec9670f

Edward Thomson 2014-01-08T10:07:30

Handle git_buf's from users more liberally

diff --git a/include/git2/blob.h b/include/git2/blob.h
index 19ad4d9..6ba5e9f 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -84,7 +84,7 @@ GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob);
  * time.
  *
  * @param blob pointer to the blob
- * @return the pointer; NULL if the blob has no contents
+ * @return the pointer
  */
 GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
 
diff --git a/src/blob.c b/src/blob.c
index ab344ae..2e924f3 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -347,6 +347,8 @@ int git_blob_filtered_content(
 
 	assert(blob && path && out);
 
+	git_buf_sanitize(out);
+
 	if (check_for_binary_data && git_blob_is_binary(blob))
 		return 0;
 
diff --git a/src/buffer.c b/src/buffer.c
index 2068232..3283c2d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -100,6 +100,14 @@ void git_buf_free(git_buf *buf)
 	git_buf_init(buf, 0);
 }
 
+void git_buf_sanitize(git_buf *buf)
+{
+	if (buf->ptr == NULL) {
+		assert (buf->size == 0 && buf->asize == 0);
+		buf->ptr = git_buf__initbuf;
+	}
+}
+
 void git_buf_clear(git_buf *buf)
 {
 	buf->size = 0;
diff --git a/src/buffer.h b/src/buffer.h
index c88af6f..564a4f5 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -51,6 +51,15 @@ extern void git_buf_init(git_buf *buf, size_t initial_size);
 extern int git_buf_try_grow(
 	git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external);
 
+/**
+ * Sanitizes git_buf structures provided from user input.  Users of the
+ * library, when providing git_buf's, may wish to provide a NULL ptr for
+ * ease of handling.  The buffer routines, however, expect a non-NULL ptr
+ * always.  This helper method simply handles NULL input, converting to a
+ * git_buf__initbuf.
+ */
+extern void git_buf_sanitize(git_buf *buf);
+
 extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
 extern char *git_buf_detach(git_buf *buf);
 extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
diff --git a/tests/filter/blob.c b/tests/filter/blob.c
index 9600a97..8dce647 100644
--- a/tests/filter/blob.c
+++ b/tests/filter/blob.c
@@ -47,6 +47,38 @@ void test_filter_blob__all_crlf(void)
 	git_blob_free(blob);
 }
 
+void test_filter_blob__sanitizes(void)
+{
+	git_blob *blob;
+	git_buf buf;
+
+	cl_git_pass(git_revparse_single(
+		(git_object **)&blob, g_repo, "e69de29")); /* zero-byte */
+
+	cl_assert_equal_i(0, git_blob_rawsize(blob));
+	cl_assert_equal_s("", git_blob_rawcontent(blob));
+
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1));
+	cl_assert_equal_sz(0, buf.size);
+	cl_assert_equal_s("", buf.ptr);
+	git_buf_free(&buf);
+
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1));
+	cl_assert_equal_sz(0, buf.size);
+	cl_assert_equal_s("", buf.ptr);
+	git_buf_free(&buf);
+
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));
+	cl_assert_equal_sz(0, buf.size);
+	cl_assert_equal_s("", buf.ptr);
+	git_buf_free(&buf);
+
+	git_blob_free(blob);
+}
+
 void test_filter_blob__ident(void)
 {
 	git_oid id;
diff --git a/tests/resources/crlf/.gitted/objects/2c/9a868cfdf8e270d0ec68164433376c68fb1789 b/tests/resources/crlf/.gitted/objects/2c/9a868cfdf8e270d0ec68164433376c68fb1789
new file mode 100644
index 0000000..218e9d1
Binary files /dev/null and b/tests/resources/crlf/.gitted/objects/2c/9a868cfdf8e270d0ec68164433376c68fb1789 differ
diff --git a/tests/resources/crlf/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/tests/resources/crlf/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
new file mode 100644
index 0000000..7112238
Binary files /dev/null and b/tests/resources/crlf/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 differ
diff --git a/tests/resources/crlf/.gitted/objects/ef/0dcd356d77221e9c27f4f3928ad28e80b87ceb b/tests/resources/crlf/.gitted/objects/ef/0dcd356d77221e9c27f4f3928ad28e80b87ceb
new file mode 100644
index 0000000..33aceda
Binary files /dev/null and b/tests/resources/crlf/.gitted/objects/ef/0dcd356d77221e9c27f4f3928ad28e80b87ceb differ
diff --git a/tests/resources/crlf/.gitted/refs/heads/master b/tests/resources/crlf/.gitted/refs/heads/master
index a2dbe0c..cfdaaf3 100644
--- a/tests/resources/crlf/.gitted/refs/heads/master
+++ b/tests/resources/crlf/.gitted/refs/heads/master
@@ -1 +1 @@
-12faf3c1ea55f572473cec9052fca468c3584ccb
+2c9a868cfdf8e270d0ec68164433376c68fb1789