Skip to content

Commit a52b4c5

Browse files
committed
odb: fix writing to fake write streams
In commit 7ec7aa4 (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.
1 parent 904307a commit a52b4c5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/odb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t
369369
{
370370
fake_wstream *stream = (fake_wstream *)_stream;
371371

372-
assert(stream->written + len > stream->size);
372+
assert(stream->written + len <= stream->size);
373373

374374
memcpy(stream->buffer + stream->written, data, len);
375375
stream->written += len;

tests/odb/backend/mempack.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ void test_odb_backend_mempack__exists_with_existing_objects_succeeds(void)
5050
cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJ_BLOB));
5151
cl_assert(git_odb_exists(_odb, &_oid) == 1);
5252
}
53+
54+
void test_odb_backend_mempack__blob_create_frombuffer_succeeds(void)
55+
{
56+
const char *data = "data";
57+
58+
cl_git_pass(git_blob_create_frombuffer(&_oid, _repo, data, strlen(data) + 1));
59+
cl_assert(git_odb_exists(_odb, &_oid) == 1);
60+
}

0 commit comments

Comments
 (0)