libgit2/libgit2 — pull request #1602 by arrbee (2013-05-23 18:18:16+00:00)
Title: Get paranoid!

Okay, so the [GitHub for Mac](http://mac.github.com/) folks have been seeing some strange crashes while using the library. I suspect the problem comes from accidentally using freed objects or the like, but it has proven to be pretty tricky to chase down. I'd like to be able to detect or trigger these problems more quickly and get some more definitive information about whether we are looking at memory corruption or misuse of library objects.

Many of the objects that we allocate are prefixed with a `git_refcount` struct that is used to track the reference count (surprise!). This PR hijacks that structure to insert a magic "signature" value at the beginning of every object which is set when the object is allocated and cleared when it is freed. This will allow us to detect very quickly when an invalid object is used. It won't prevent crashes in all cases, because if you are really using invalid memory then you'll probably still get a crash, but in the case where memory is reused and we read random data, this should allow us to catch it right away.

There are a couple of other "paranoia" changes in this PR that are related to the crashes that are showing up. Mostly they are just doing extra checking for NULL pointers and the like.

This also contains a change to refactor some config lookups out of the inner loop of formatting a diff. Because that was the locus of one of the crashes and because it is unnecessary work, I thought that moving that code was also a good idea. Even if everyone hates this PR, that commit should probably be pulled out and merged separately.