Skip to content

Commit ef49a7a

Browse files
committed
zlib: zlib can only process 4GB at a time
The size of objects we read from the repository and data we try to put into the repository are represented in "unsigned long", so that on larger architectures we can handle objects that weigh more than 4GB. But the interface defined in zlib.h to communicate with inflate/deflate limits avail_in (how many bytes of input are we calling zlib with) and avail_out (how many bytes of output from zlib are we ready to accept) fields effectively to 4GB by defining their type to be uInt. In many places in our code, we allocate a large buffer (e.g. mmap'ing a large loose object file) and tell zlib its size by assigning the size to avail_in field of the stream, but that will truncate the high octets of the real size. The worst part of this story is that we often pass around z_stream (the state object used by zlib) to keep track of the number of used bytes in input/output buffer by inspecting these two fields, which practically limits our callchain to the same 4GB limit. Wrap z_stream in another structure git_zstream that can express avail_in and avail_out in unsigned long. For now, just die() when the caller gives a size that cannot be given to a single zlib call. In later patches in the series, we would make git_inflate() and git_deflate() internally loop to give callers an illusion that our "improved" version of zlib interface can operate on a buffer larger than 4GB in one go. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 225a6f1 commit ef49a7a

15 files changed

+142
-70
lines changed

archive-zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void copy_le32(unsigned char *dest, unsigned int n)
9090
static void *zlib_deflate(void *data, unsigned long size,
9191
int compression_level, unsigned long *compressed_size)
9292
{
93-
z_stream stream;
93+
git_zstream stream;
9494
unsigned long maxsize;
9595
void *buffer;
9696
int result;

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ static inline int metadata_changes(struct patch *patch)
16341634
static char *inflate_it(const void *data, unsigned long size,
16351635
unsigned long inflated_size)
16361636
{
1637-
z_stream stream;
1637+
git_zstream stream;
16381638
void *out;
16391639
int st;
16401640

builtin/index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void unlink_base_data(struct base_data *c)
265265
static void *unpack_entry_data(unsigned long offset, unsigned long size)
266266
{
267267
int status;
268-
z_stream stream;
268+
git_zstream stream;
269269
void *buf = xmalloc(size);
270270

271271
memset(&stream, 0, sizeof(stream));
@@ -355,7 +355,7 @@ static void *get_data_from_pack(struct object_entry *obj)
355355
off_t from = obj[0].idx.offset + obj[0].hdr_size;
356356
unsigned long len = obj[1].idx.offset - from;
357357
unsigned char *data, *inbuf;
358-
z_stream stream;
358+
git_zstream stream;
359359
int status;
360360

361361
data = xmalloc(obj->size);
@@ -666,7 +666,7 @@ static void parse_pack_objects(unsigned char *sha1)
666666

667667
static int write_compressed(struct sha1file *f, void *in, unsigned int size)
668668
{
669-
z_stream stream;
669+
git_zstream stream;
670670
int status;
671671
unsigned char outbuf[4096];
672672

builtin/pack-objects.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static void *get_delta(struct object_entry *entry)
126126

127127
static unsigned long do_compress(void **pptr, unsigned long size)
128128
{
129-
z_stream stream;
129+
git_zstream stream;
130130
void *in, *out;
131131
unsigned long maxsize;
132132

@@ -160,7 +160,7 @@ static int check_pack_inflate(struct packed_git *p,
160160
off_t len,
161161
unsigned long expect)
162162
{
163-
z_stream stream;
163+
git_zstream stream;
164164
unsigned char fakebuf[4096], *in;
165165
int st;
166166

@@ -187,12 +187,12 @@ static void copy_pack_data(struct sha1file *f,
187187
off_t len)
188188
{
189189
unsigned char *in;
190-
unsigned int avail;
190+
unsigned long avail;
191191

192192
while (len) {
193193
in = use_pack(p, w_curs, offset, &avail);
194194
if (avail > len)
195-
avail = (unsigned int)len;
195+
avail = (unsigned long)len;
196196
sha1write(f, in, avail);
197197
offset += avail;
198198
len -= avail;
@@ -994,7 +994,7 @@ static void check_object(struct object_entry *entry)
994994
const unsigned char *base_ref = NULL;
995995
struct object_entry *base_entry;
996996
unsigned long used, used_0;
997-
unsigned int avail;
997+
unsigned long avail;
998998
off_t ofs;
999999
unsigned char *buf, c;
10001000

builtin/unpack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void use(int bytes)
9090

9191
static void *get_data(unsigned long size)
9292
{
93-
z_stream stream;
93+
git_zstream stream;
9494
void *buf = xmalloc(size);
9595

9696
memset(&stream, 0, sizeof(stream));

cache.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@
1616
#endif
1717

1818
#include <zlib.h>
19-
20-
void git_inflate_init(z_streamp strm);
21-
void git_inflate_init_gzip_only(z_streamp strm);
22-
void git_inflate_end(z_streamp strm);
23-
int git_inflate(z_streamp strm, int flush);
24-
25-
void git_deflate_init(z_streamp strm, int level);
26-
void git_deflate_init_gzip(z_streamp strm, int level);
27-
void git_deflate_end(z_streamp strm);
28-
int git_deflate_end_gently(z_streamp strm);
29-
int git_deflate(z_streamp strm, int flush);
30-
unsigned long git_deflate_bound(z_streamp, unsigned long);
19+
typedef struct git_zstream {
20+
z_stream z;
21+
unsigned long avail_in;
22+
unsigned long avail_out;
23+
unsigned long total_in;
24+
unsigned long total_out;
25+
unsigned char *next_in;
26+
unsigned char *next_out;
27+
} git_zstream;
28+
29+
void git_inflate_init(git_zstream *);
30+
void git_inflate_init_gzip_only(git_zstream *);
31+
void git_inflate_end(git_zstream *);
32+
int git_inflate(git_zstream *, int flush);
33+
34+
void git_deflate_init(git_zstream *, int level);
35+
void git_deflate_init_gzip(git_zstream *, int level);
36+
void git_deflate_end(git_zstream *);
37+
int git_deflate_end_gently(git_zstream *);
38+
int git_deflate(git_zstream *, int flush);
39+
unsigned long git_deflate_bound(git_zstream *, unsigned long);
3140

3241
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
3342
#define DTYPE(de) ((de)->d_type)
@@ -991,7 +1000,7 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
9911000
extern void pack_report(void);
9921001
extern int open_pack_index(struct packed_git *);
9931002
extern void close_pack_index(struct packed_git *);
994-
extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
1003+
extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
9951004
extern void close_pack_windows(struct packed_git *);
9961005
extern void unuse_pack(struct pack_window **);
9971006
extern void free_pack_by_name(const char *);

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ static unsigned char *deflate_it(char *data,
17291729
{
17301730
int bound;
17311731
unsigned char *deflated;
1732-
z_stream stream;
1732+
git_zstream stream;
17331733

17341734
memset(&stream, 0, sizeof(stream));
17351735
git_deflate_init(&stream, zlib_compression_level);

fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ static int store_object(
10171017
unsigned char sha1[20];
10181018
unsigned long hdrlen, deltalen;
10191019
git_SHA_CTX c;
1020-
z_stream s;
1020+
git_zstream s;
10211021

10221022
hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
10231023
(unsigned long)dat->len) + 1;
@@ -1163,7 +1163,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
11631163
off_t offset;
11641164
git_SHA_CTX c;
11651165
git_SHA_CTX pack_file_ctx;
1166-
z_stream s;
1166+
git_zstream s;
11671167
int status = Z_OK;
11681168

11691169
/* Determine if we should auto-checkpoint. */

http-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static struct rpc_service *select_service(const char *name)
271271

272272
static void inflate_request(const char *prog_name, int out)
273273
{
274-
z_stream stream;
274+
git_zstream stream;
275275
unsigned char in_buf[8192];
276276
unsigned char out_buf[8192];
277277
unsigned long cnt = 0;

http-push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static void start_put(struct transfer_request *request)
352352
unsigned long len;
353353
int hdrlen;
354354
ssize_t size;
355-
z_stream stream;
355+
git_zstream stream;
356356

357357
unpacked = read_sha1_file(request->obj->sha1, &type, &len);
358358
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;

0 commit comments

Comments
 (0)