libgit2/rugged — Issue #41: git_tree_create_from_index: Segmentation Fault
Author: blue-dev
Created: 2012-01-24 21:03:25+00:00
State: open

Below is a minimal program which crashed with a segmentation fault for me.
I'm using the latest version of libgit2 from the repositories.

My sample repository "repo" is an empty repository, containing the empty file "test"
which I want to add to the index and then commit later on.

```C++
int main() {
git_repository *repo = NULL;
git_repository_open(&repo, "repo");

git_index *index;
git_repository_index(&index, repo);
git_index_read(index);
git_index_add(index, "test", 0);
git_index_write(index);

git_oid *oid;
git_tree_create_fromindex(oid, index); //crash with a segmentation fault
[...]
}
```

I tried to find the exact line where it crashes, this is the order of calls:
1. git_tree_create_fromindex
2. write_tree
3. git_treebuilder_write
4. git_odb_write

And it crashes in line 612 in odb.c ("error = b->write(oid, b, data, len, type);").
If I comment out the b->write line including the if statement, the program crashes
on line 682 ("error = stream->finalize_write(oid, stream);").