Be more careful with user-supplied buffers

Proposed fix for #2333 

1. Add missing calls to `git_buf_sanitize` on public APIs
2. Fix places where `git_buf` APIs could incorrectly write NUL terminators into invalid buffers
3. Change behavior of `git_buf_sanitize` to NUL terminate a buffer if it can
4. Change `git_buf_shorten` to just use `git_buf_truncate` or `git_buf_clear`
5. Adds tests of filtering code with zeroed (i.e. unsanitized) buffer which was segfaulting