{"thread":{"id":"38716","subject":"[PATCH] zlib: initialize git_zstream in git_deflate_init{,_gzip,_raw}","startedAt":"2015-03-05T22:49:46Z","lastAt":"2015-03-05T23:02:49Z","messageCount":2,"participants":["René Scharfe","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"257115","messageId":"54F8DD8A.8020202@web.de","threadId":"38716","inReplyTo":null,"subject":"[PATCH] zlib: initialize git_zstream in git_deflate_init{,_gzip,_raw}","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2015-03-05T22:49:46Z","receivedAt":"2015-03-05T22:49:46Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Clear the git_zstream variable at the start of git_deflate_init() etc.\nso that callers don't have to do that.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n archive-zip.c          | 2 --\n builtin/index-pack.c   | 1 -\n builtin/pack-objects.c | 2 --\n bulk-checkin.c         | 1 -\n diff.c                 | 1 -\n fast-import.c          | 3 ---\n http-push.c            | 1 -\n remote-curl.c          | 1 -\n sha1_file.c            | 1 -\n zlib.c                 | 2 ++\n 10 files changed, 2 insertions(+), 13 deletions(-)\n\ndiff --git a/archive-zip.c b/archive-zip.c\nindex 4bde019..1a54e1b 100644\n--- a/archive-zip.c\n+++ b/archive-zip.c\n@@ -120,7 +120,6 @@ static void *zlib_deflate_raw(void *data, unsigned long size,\n \tvoid *buffer;\n \tint result;\n \n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init_raw(&stream, compression_level);\n \tmaxsize = git_deflate_bound(&stream, size);\n \tbuffer = xmalloc(maxsize);\n@@ -349,7 +348,6 @@ static int write_zip_entry(struct archiver_args *args,\n \t\tsize_t out_len;\n \t\tunsigned char compressed[STREAM_BUFFER_SIZE * 2];\n \n-\t\tmemset(&zstream, 0, sizeof(zstream));\n \t\tgit_deflate_init_raw(&zstream, args->compression_level);\n \n \t\tcompressed_size = 0;\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex 4632117..cf654df 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -1204,7 +1204,6 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)\n \tint status;\n \tunsigned char outbuf[4096];\n \n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, zlib_compression_level);\n \tstream.next_in = in;\n \tstream.avail_in = size;\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex d816587..c3a7516 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -125,7 +125,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)\n \tvoid *in, *out;\n \tunsigned long maxsize;\n \n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, pack_compression_level);\n \tmaxsize = git_deflate_bound(&stream, size);\n \n@@ -153,7 +152,6 @@ static unsigned long write_large_blob_data(struct git_istream *st, struct sha1fi\n \tunsigned char obuf[1024 * 16];\n \tunsigned long olen = 0;\n \n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, pack_compression_level);\n \n \tfor (;;) {\ndiff --git a/bulk-checkin.c b/bulk-checkin.c\nindex 0c4b8a7..8d157eb 100644\n--- a/bulk-checkin.c\n+++ b/bulk-checkin.c\n@@ -105,7 +105,6 @@ static int stream_to_pack(struct bulk_checkin_state *state,\n \tint write_object = (flags & HASH_WRITE_OBJECT);\n \toff_t offset = 0;\n \n-\tmemset(&s, 0, sizeof(s));\n \tgit_deflate_init(&s, pack_compression_level);\n \n \thdrlen = encode_in_pack_object_header(type, size, obuf);\ndiff --git a/diff.c b/diff.c\nindex d1bd534..dad875c 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -2093,7 +2093,6 @@ static unsigned char *deflate_it(char *data,\n \tunsigned char *deflated;\n \tgit_zstream stream;\n \n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, zlib_compression_level);\n \tbound = git_deflate_bound(&stream, size);\n \tdeflated = xmalloc(bound);\ndiff --git a/fast-import.c b/fast-import.c\nindex aac2c24..77fb2ff 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1062,7 +1062,6 @@ static int store_object(\n \t} else\n \t\tdelta = NULL;\n \n-\tmemset(&s, 0, sizeof(s));\n \tgit_deflate_init(&s, pack_compression_level);\n \tif (delta) {\n \t\ts.next_in = delta;\n@@ -1090,7 +1089,6 @@ static int store_object(\n \t\t\tfree(delta);\n \t\t\tdelta = NULL;\n \n-\t\t\tmemset(&s, 0, sizeof(s));\n \t\t\tgit_deflate_init(&s, pack_compression_level);\n \t\t\ts.next_in = (void *)dat->buf;\n \t\t\ts.avail_in = dat->len;\n@@ -1190,7 +1188,6 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)\n \n \tcrc32_begin(pack_file);\n \n-\tmemset(&s, 0, sizeof(s));\n \tgit_deflate_init(&s, pack_compression_level);\n \n \thdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);\ndiff --git a/http-push.c b/http-push.c\nindex 0beb7ab..bfb1c96 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -365,7 +365,6 @@ static void start_put(struct transfer_request *request)\n \thdrlen = sprintf(hdr, \"%s %lu\", typename(type), len) + 1;\n \n \t/* Set it up */\n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, zlib_compression_level);\n \tsize = git_deflate_bound(&stream, len + hdrlen);\n \tstrbuf_init(&request->buffer.buf, size);\ndiff --git a/remote-curl.c b/remote-curl.c\nindex deb4bfe..af7b678 100644\n--- a/remote-curl.c\n+++ b/remote-curl.c\n@@ -567,7 +567,6 @@ retry:\n \t\tgit_zstream stream;\n \t\tint ret;\n \n-\t\tmemset(&stream, 0, sizeof(stream));\n \t\tgit_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);\n \t\tgzip_size = git_deflate_bound(&stream, rpc->len);\n \t\tgzip_body = xmalloc(gzip_size);\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 69a60ec..88f06ba 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2943,7 +2943,6 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n \t}\n \n \t/* Set it up */\n-\tmemset(&stream, 0, sizeof(stream));\n \tgit_deflate_init(&stream, zlib_compression_level);\n \tstream.next_out = compressed;\n \tstream.avail_out = sizeof(compressed);\ndiff --git a/zlib.c b/zlib.c\nindex 61e6df0..4223f1a 100644\n--- a/zlib.c\n+++ b/zlib.c\n@@ -159,6 +159,7 @@ void git_deflate_init(git_zstream *strm, int level)\n {\n \tint status;\n \n+\tmemset(strm, 0, sizeof(*strm));\n \tzlib_pre_call(strm);\n \tstatus = deflateInit(&strm->z, level);\n \tzlib_post_call(strm);\n@@ -172,6 +173,7 @@ static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)\n {\n \tint status;\n \n+\tmemset(strm, 0, sizeof(*strm));\n \tzlib_pre_call(strm);\n \tstatus = deflateInit2(&strm->z, level,\n \t\t\t\t  Z_DEFLATED, windowBits,\n-- \n2.3.1\n"},{"id":"257116","messageId":"xmqqd24n1206.fsf@gitster.dls.corp.google.com","threadId":"38716","inReplyTo":"54F8DD8A.8020202@web.de","subject":"Re: [PATCH] zlib: initialize git_zstream in git_deflate_init{,_gzip,_raw}","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-03-05T23:02:49Z","receivedAt":"2015-03-05T23:02:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> Clear the git_zstream variable at the start of git_deflate_init() etc.\n> so that callers don't have to do that.\n>\n> Signed-off-by: Rene Scharfe <l.s.r@web.de>\n\nNice.  \"git grep -B1 git_deflate_init\" tells me that all existing\ncallers of the function do this memset().  Clearly the above is a\nvery good idea and is a safe and sane thing to do.\n\nThanks.\n\n> ---\n>  archive-zip.c          | 2 --\n>  builtin/index-pack.c   | 1 -\n>  builtin/pack-objects.c | 2 --\n>  bulk-checkin.c         | 1 -\n>  diff.c                 | 1 -\n>  fast-import.c          | 3 ---\n>  http-push.c            | 1 -\n>  remote-curl.c          | 1 -\n>  sha1_file.c            | 1 -\n>  zlib.c                 | 2 ++\n>  10 files changed, 2 insertions(+), 13 deletions(-)\n>\n> diff --git a/archive-zip.c b/archive-zip.c\n> index 4bde019..1a54e1b 100644\n> --- a/archive-zip.c\n> +++ b/archive-zip.c\n> @@ -120,7 +120,6 @@ static void *zlib_deflate_raw(void *data, unsigned long size,\n>  \tvoid *buffer;\n>  \tint result;\n>  \n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init_raw(&stream, compression_level);\n>  \tmaxsize = git_deflate_bound(&stream, size);\n>  \tbuffer = xmalloc(maxsize);\n> @@ -349,7 +348,6 @@ static int write_zip_entry(struct archiver_args *args,\n>  \t\tsize_t out_len;\n>  \t\tunsigned char compressed[STREAM_BUFFER_SIZE * 2];\n>  \n> -\t\tmemset(&zstream, 0, sizeof(zstream));\n>  \t\tgit_deflate_init_raw(&zstream, args->compression_level);\n>  \n>  \t\tcompressed_size = 0;\n> diff --git a/builtin/index-pack.c b/builtin/index-pack.c\n> index 4632117..cf654df 100644\n> --- a/builtin/index-pack.c\n> +++ b/builtin/index-pack.c\n> @@ -1204,7 +1204,6 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)\n>  \tint status;\n>  \tunsigned char outbuf[4096];\n>  \n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, zlib_compression_level);\n>  \tstream.next_in = in;\n>  \tstream.avail_in = size;\n> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\n> index d816587..c3a7516 100644\n> --- a/builtin/pack-objects.c\n> +++ b/builtin/pack-objects.c\n> @@ -125,7 +125,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)\n>  \tvoid *in, *out;\n>  \tunsigned long maxsize;\n>  \n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, pack_compression_level);\n>  \tmaxsize = git_deflate_bound(&stream, size);\n>  \n> @@ -153,7 +152,6 @@ static unsigned long write_large_blob_data(struct git_istream *st, struct sha1fi\n>  \tunsigned char obuf[1024 * 16];\n>  \tunsigned long olen = 0;\n>  \n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, pack_compression_level);\n>  \n>  \tfor (;;) {\n> diff --git a/bulk-checkin.c b/bulk-checkin.c\n> index 0c4b8a7..8d157eb 100644\n> --- a/bulk-checkin.c\n> +++ b/bulk-checkin.c\n> @@ -105,7 +105,6 @@ static int stream_to_pack(struct bulk_checkin_state *state,\n>  \tint write_object = (flags & HASH_WRITE_OBJECT);\n>  \toff_t offset = 0;\n>  \n> -\tmemset(&s, 0, sizeof(s));\n>  \tgit_deflate_init(&s, pack_compression_level);\n>  \n>  \thdrlen = encode_in_pack_object_header(type, size, obuf);\n> diff --git a/diff.c b/diff.c\n> index d1bd534..dad875c 100644\n> --- a/diff.c\n> +++ b/diff.c\n> @@ -2093,7 +2093,6 @@ static unsigned char *deflate_it(char *data,\n>  \tunsigned char *deflated;\n>  \tgit_zstream stream;\n>  \n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, zlib_compression_level);\n>  \tbound = git_deflate_bound(&stream, size);\n>  \tdeflated = xmalloc(bound);\n> diff --git a/fast-import.c b/fast-import.c\n> index aac2c24..77fb2ff 100644\n> --- a/fast-import.c\n> +++ b/fast-import.c\n> @@ -1062,7 +1062,6 @@ static int store_object(\n>  \t} else\n>  \t\tdelta = NULL;\n>  \n> -\tmemset(&s, 0, sizeof(s));\n>  \tgit_deflate_init(&s, pack_compression_level);\n>  \tif (delta) {\n>  \t\ts.next_in = delta;\n> @@ -1090,7 +1089,6 @@ static int store_object(\n>  \t\t\tfree(delta);\n>  \t\t\tdelta = NULL;\n>  \n> -\t\t\tmemset(&s, 0, sizeof(s));\n>  \t\t\tgit_deflate_init(&s, pack_compression_level);\n>  \t\t\ts.next_in = (void *)dat->buf;\n>  \t\t\ts.avail_in = dat->len;\n> @@ -1190,7 +1188,6 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)\n>  \n>  \tcrc32_begin(pack_file);\n>  \n> -\tmemset(&s, 0, sizeof(s));\n>  \tgit_deflate_init(&s, pack_compression_level);\n>  \n>  \thdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);\n> diff --git a/http-push.c b/http-push.c\n> index 0beb7ab..bfb1c96 100644\n> --- a/http-push.c\n> +++ b/http-push.c\n> @@ -365,7 +365,6 @@ static void start_put(struct transfer_request *request)\n>  \thdrlen = sprintf(hdr, \"%s %lu\", typename(type), len) + 1;\n>  \n>  \t/* Set it up */\n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, zlib_compression_level);\n>  \tsize = git_deflate_bound(&stream, len + hdrlen);\n>  \tstrbuf_init(&request->buffer.buf, size);\n> diff --git a/remote-curl.c b/remote-curl.c\n> index deb4bfe..af7b678 100644\n> --- a/remote-curl.c\n> +++ b/remote-curl.c\n> @@ -567,7 +567,6 @@ retry:\n>  \t\tgit_zstream stream;\n>  \t\tint ret;\n>  \n> -\t\tmemset(&stream, 0, sizeof(stream));\n>  \t\tgit_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);\n>  \t\tgzip_size = git_deflate_bound(&stream, rpc->len);\n>  \t\tgzip_body = xmalloc(gzip_size);\n> diff --git a/sha1_file.c b/sha1_file.c\n> index 69a60ec..88f06ba 100644\n> --- a/sha1_file.c\n> +++ b/sha1_file.c\n> @@ -2943,7 +2943,6 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n>  \t}\n>  \n>  \t/* Set it up */\n> -\tmemset(&stream, 0, sizeof(stream));\n>  \tgit_deflate_init(&stream, zlib_compression_level);\n>  \tstream.next_out = compressed;\n>  \tstream.avail_out = sizeof(compressed);\n> diff --git a/zlib.c b/zlib.c\n> index 61e6df0..4223f1a 100644\n> --- a/zlib.c\n> +++ b/zlib.c\n> @@ -159,6 +159,7 @@ void git_deflate_init(git_zstream *strm, int level)\n>  {\n>  \tint status;\n>  \n> +\tmemset(strm, 0, sizeof(*strm));\n>  \tzlib_pre_call(strm);\n>  \tstatus = deflateInit(&strm->z, level);\n>  \tzlib_post_call(strm);\n> @@ -172,6 +173,7 @@ static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)\n>  {\n>  \tint status;\n>  \n> +\tmemset(strm, 0, sizeof(*strm));\n>  \tzlib_pre_call(strm);\n>  \tstatus = deflateInit2(&strm->z, level,\n>  \t\t\t\t  Z_DEFLATED, windowBits,\n"}]}