diff options
author | Russell Belfer <arrbee@arrbee.com> | 2011-11-18 03:10:27 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-11-22 04:53:56 +0400 |
commit | b762e576c6d0118664320f50be2e5810dbed4c15 (patch) | |
tree | 2c244a57ca5167386856c573336093744464daa6 /src/filebuf.c | |
parent | 1d09a1c88ddde32bf9b6b5ab6a9feab2ecdaa6ff (diff) |
filebuf: add GIT_FILEBUF_INIT and protect multiple opens and cleanups
Update all stack allocations of git_filebuf to use GIT_FILEBUF_INIT
and make git_filebuf_open and git_filebuf_cleanup safe to be called
multiple times on the same buffer.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/filebuf.c')
-rw-r--r-- | src/filebuf.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/filebuf.c b/src/filebuf.c index 199418032..6600bfa4b 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -66,13 +66,22 @@ void git_filebuf_cleanup(git_filebuf *file) if (file->digest) git_hash_free_ctx(file->digest); - git__free(file->buffer); - git__free(file->z_buf); + if (file->buffer) + git__free(file->buffer); - deflateEnd(&file->zs); + /* use the presence of z_buf to decide if we need to deflateEnd */ + if (file->z_buf) { + git__free(file->z_buf); + deflateEnd(&file->zs); + } - git__free(file->path_original); - git__free(file->path_lock); + if (file->path_original) + git__free(file->path_original); + if (file->path_lock) + git__free(file->path_lock); + + memset(file, 0x0, sizeof(git_filebuf)); + file->fd = -1; } GIT_INLINE(int) flush_buffer(git_filebuf *file) @@ -137,6 +146,9 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags) assert(file && path); + if (file->buffer) + return git__throw(GIT_EINVALIDARGS, "Tried to reopen an open filebuf"); + memset(file, 0x0, sizeof(git_filebuf)); file->buf_size = WRITE_BUFFER_SIZE; |