Skip to content

Commit 5e86c1f

Browse files
committed
zlib: wrap inflateInit2 used to accept only for gzip format
http-backend.c uses inflateInit2() to tell the library that it wants to accept only gzip format. Wrap it in a helper function so that readers do not have to wonder what the magic numbers 15 and 16 are for. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9e7e5ca commit 5e86c1f

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#endif
2222

2323
void git_inflate_init(z_streamp strm);
24+
void git_inflate_init_gzip_only(z_streamp strm);
2425
void git_inflate_end(z_streamp strm);
2526
int git_inflate(z_streamp strm, int flush);
2627

http-backend.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,9 @@ static void inflate_request(const char *prog_name, int out)
275275
unsigned char in_buf[8192];
276276
unsigned char out_buf[8192];
277277
unsigned long cnt = 0;
278-
int ret;
279278

280279
memset(&stream, 0, sizeof(stream));
281-
ret = inflateInit2(&stream, (15 + 16));
282-
if (ret != Z_OK)
283-
die("cannot start zlib inflater, zlib err %d", ret);
280+
git_inflate_init_gzip_only(&stream);
284281

285282
while (1) {
286283
ssize_t n = xread(0, in_buf, sizeof(in_buf));

zlib.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ void git_inflate_init(z_streamp strm)
3232
strm->msg ? strm->msg : "no message");
3333
}
3434

35+
void git_inflate_init_gzip_only(z_streamp strm)
36+
{
37+
/*
38+
* Use default 15 bits, +16 is to accept only gzip and to
39+
* yield Z_DATA_ERROR when fed zlib format.
40+
*/
41+
const int windowBits = 15 + 16;
42+
int status = inflateInit2(strm, windowBits);
43+
44+
if (status == Z_OK)
45+
return;
46+
die("inflateInit2: %s (%s)", zerr_to_string(status),
47+
strm->msg ? strm->msg : "no message");
48+
}
49+
3550
void git_inflate_end(z_streamp strm)
3651
{
3752
int status = inflateEnd(strm);

0 commit comments

Comments
 (0)