Commit a52b4c51c0a7de341f5b2cef48ac6d7c8b9476e1

Patrick Steinhardt 2018-03-23T09:59:46

odb: fix writing to fake write streams In commit 7ec7aa4a7 (odb: assert on logic errors when writing objects, 2018-02-01), the check for whether we are trying to overflowing the fake stream buffer was changed from returning an error to raising an assert. The conversion forgot though that the logic around `assert`s are basically inverted. Previously, if the statement stream->written + len > steram->size evaluated to true, we would return a `-1`. Now we are asserting that this statement is true, and in case it is not we will raise an error. So the conversion to the `assert` in fact changed the behaviour to the complete opposite intention. Fix the assert by inverting its condition again and add a regression test.

diff --git a/src/odb.c b/src/odb.c
index 07206c1..ef9c875 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -369,7 +369,7 @@ static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t
 {
 	fake_wstream *stream = (fake_wstream *)_stream;
 
-	assert(stream->written + len > stream->size);
+	assert(stream->written + len <= stream->size);
 
 	memcpy(stream->buffer + stream->written, data, len);
 	stream->written += len;
diff --git a/tests/odb/backend/mempack.c b/tests/odb/backend/mempack.c
index 46c685c..624f0e6 100644
--- a/tests/odb/backend/mempack.c
+++ b/tests/odb/backend/mempack.c
@@ -50,3 +50,11 @@ void test_odb_backend_mempack__exists_with_existing_objects_succeeds(void)
 	cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJ_BLOB));
 	cl_assert(git_odb_exists(_odb, &_oid) == 1);
 }
+
+void test_odb_backend_mempack__blob_create_frombuffer_succeeds(void)
+{
+	const char *data = "data";
+
+	cl_git_pass(git_blob_create_frombuffer(&_oid, _repo, data, strlen(data) + 1));
+	cl_assert(git_odb_exists(_odb, &_oid) == 1);
+}