Commit 19459b1e29e150c02e733bdefd5e1eb09fdd4bf7

Russell Belfer 2014-01-30T10:23:35

Defer zstream NUL termination to end And don't terminate if there isn't space for it (since it's binary data, it's not worth a reallocation).

diff --git a/src/zstream.c b/src/zstream.c
index 82ae5e6..85fa2e0 100644
--- a/src/zstream.c
+++ b/src/zstream.c
@@ -134,7 +134,7 @@ int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
 	while (!git_zstream_done(&zs)) {
 		size_t step = git_zstream_suggest_output_len(&zs), written;
 
-		if ((error = git_buf_grow(out, out->asize + step + 1)) < 0)
+		if ((error = git_buf_grow(out, out->asize + step)) < 0)
 			goto done;
 
 		written = out->asize - out->size;
@@ -144,9 +144,12 @@ int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
 			goto done;
 
 		out->size += written;
-		out->ptr[out->size] = '\0';
 	}
 
+	/* NULL terminate for consistency if possible */
+	if (out->size < out->asize)
+		out->ptr[out->size] = '\0';
+
 done:
 	git_zstream_free(&zs);
 	return error;