libgit2/libgit2 — pull request #1535 by carlosmn (2013-05-02 16:34:49+00:00)
Title: Switch to index_version as "git_pack_file is ready" flag

We use `p->index_map.data` to check whether the struct has been set up and all the information about the index is stored there. This variable gets set up halfway through the setup process, however, and a thread can come along and use fields that haven't been written to yet.

Crucially, `pack_entry_find_offset()` needs to read the index version (which is written after index_map) to know the offset and stride length to pass to `sha1_entry_pos()`. If these values are wrong, assertions in it will fail, as it will be reading bogus data.

Make index_version the last field to be written and switch from using `p->index_map.data` to `p->index_version` as "`git_pack_file` is ready" flag as we can use it to know if every field has been written.

---

I wanted to use `p->num_objects` originally as it was already the last to get written, but then I got worried about what might happen if we open a packfile without any objects in it. So it's easier to move to `p->index_version` than try to figure out whether we want to suppor that.

This PR brought to you by valgrind, drd, helgring, lack of sleep and caffeinated beverages.