Commit 6adcaab7 by Edward Thomson

Handle git_buf's from users more liberally

parent 32309b5d
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
12faf3c1ea55f572473cec9052fca468c3584ccb
2c9a868cfdf8e270d0ec68164433376c68fb1789
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment