Skip to content

Commit 28ec3d7

Browse files
author
Philip Oakley
committed
zlib.c: refactor stream chuncking to fit uLong 32bit counting
On Windows uLong and size_t are different, being 32bit and 64bit respectively. Computations of mixed 32/64 bit types can be implementation defined leading to potential accuracy loss and error. Avoid wraparound of z.total_in and z.total_in by always starting at zero. The chunk size is kept well within 32bit limits. Ensure the z.total_in and z.total_in are _upcast_ when computing the overall avail_in and avail_out values Signed-off-by: Philip Oakley <[email protected]>
1 parent 8d28694 commit 28ec3d7

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

zlib.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,20 @@ static void zlib_pre_call(git_zstream *s)
3838
{
3939
s->z.next_in = s->next_in;
4040
s->z.next_out = s->next_out;
41-
s->z.total_in = s->total_in;
42-
s->z.total_out = s->total_out;
41+
s->z.total_in = 0;
42+
s->z.total_out = 0;
4343
s->z.avail_in = zlib_buf_cap(s->avail_in);
4444
s->z.avail_out = zlib_buf_cap(s->avail_out);
4545
}
4646

4747
static void zlib_post_call(git_zstream *s)
4848
{
49-
size_t bytes_consumed;
50-
size_t bytes_produced;
51-
52-
bytes_consumed = s->z.next_in - s->next_in;
53-
bytes_produced = s->z.next_out - s->next_out;
54-
if (s->z.total_out != s->total_out + bytes_produced)
55-
BUG("total_out mismatch");
56-
if (s->z.total_in != s->total_in + bytes_consumed)
57-
BUG("total_in mismatch");
58-
59-
s->total_out = s->z.total_out;
60-
s->total_in = s->z.total_in;
49+
s->total_out += (size_t) s->z.total_out;
50+
s->total_in += (size_t) s->z.total_in;
6151
s->next_in = s->z.next_in;
6252
s->next_out = s->z.next_out;
63-
s->avail_in -= bytes_consumed;
64-
s->avail_out -= bytes_produced;
53+
s->avail_in -= (size_t) s->z.total_in;
54+
s->avail_out -= (size_t) s->z.total_out;
6555
}
6656

6757
void git_inflate_init(git_zstream *strm)

0 commit comments

Comments
 (0)