Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/git-zlib.c
Line
Count
Source
1
/*
2
 * zlib wrappers to make sure we don't silently miss errors
3
 * at init time.
4
 */
5
#include "git-compat-util.h"
6
#include "git-zlib.h"
7
8
static const char *zerr_to_string(int status)
9
0
{
10
0
  switch (status) {
11
0
  case Z_MEM_ERROR:
12
0
    return "out of memory";
13
0
  case Z_VERSION_ERROR:
14
0
    return "wrong version";
15
0
  case Z_NEED_DICT:
16
0
    return "needs dictionary";
17
0
  case Z_DATA_ERROR:
18
0
    return "data stream error";
19
0
  case Z_STREAM_ERROR:
20
0
    return "stream consistency error";
21
0
  default:
22
0
    return "unknown error";
23
0
  }
24
0
}
25
26
/*
27
 * avail_in and avail_out in zlib are counted in uInt, which typically
28
 * limits the size of the buffer we can use to 4GB when interacting
29
 * with zlib in a single call to inflate/deflate.
30
 */
31
/* #define ZLIB_BUF_MAX ((uInt)-1) */
32
0
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
33
34
/* uLong is 32-bit on Windows, even on 64-bit systems */
35
0
#define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong)
36
static inline uInt zlib_buf_cap(size_t len)
37
0
{
38
0
  return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
39
0
}
40
41
static inline uLong zlib_uLong_cap(size_t s)
42
0
{
43
0
  return s < ULONG_MAX_VALUE ? (uLong)s : ULONG_MAX_VALUE;
44
0
}
45
46
static void zlib_pre_call(git_zstream *s)
47
0
{
48
0
  s->z.next_in = s->next_in;
49
0
  s->z.next_out = s->next_out;
50
0
  s->z.total_in = zlib_uLong_cap(s->total_in);
51
0
  s->z.total_out = zlib_uLong_cap(s->total_out);
52
0
  s->z.avail_in = zlib_buf_cap(s->avail_in);
53
0
  s->z.avail_out = zlib_buf_cap(s->avail_out);
54
0
}
55
56
static void zlib_post_call(git_zstream *s, int status)
57
0
{
58
0
  size_t bytes_consumed;
59
0
  size_t bytes_produced;
60
61
0
  bytes_consumed = s->z.next_in - s->next_in;
62
0
  bytes_produced = s->z.next_out - s->next_out;
63
  /*
64
   * zlib's total_out/total_in are uLong which may wrap for >4GB.
65
   * We track our own totals and verify only the low bits match.
66
   */
67
0
  if ((s->z.total_out & ULONG_MAX_VALUE) !=
68
0
      ((zlib_uLong_cap(s->total_out) + bytes_produced) & ULONG_MAX_VALUE))
69
0
    BUG("total_out mismatch");
70
  /*
71
   * zlib does not update total_in when it returns Z_NEED_DICT,
72
   * causing a mismatch here. Skip the sanity check in that case.
73
   */
74
0
  if (status != Z_NEED_DICT &&
75
0
      (s->z.total_in & ULONG_MAX_VALUE) !=
76
0
      ((zlib_uLong_cap(s->total_in) + bytes_consumed) & ULONG_MAX_VALUE))
77
0
    BUG("total_in mismatch");
78
79
0
  s->total_out += bytes_produced;
80
0
  s->total_in += bytes_consumed;
81
  /* zlib-ng marks `next_in` as `const`, so we have to cast it away. */
82
0
  s->next_in = (unsigned char *) s->z.next_in;
83
0
  s->next_out = s->z.next_out;
84
0
  s->avail_in -= bytes_consumed;
85
0
  s->avail_out -= bytes_produced;
86
0
}
87
88
void git_inflate_init(git_zstream *strm)
89
0
{
90
0
  int status;
91
92
0
  zlib_pre_call(strm);
93
0
  status = inflateInit(&strm->z);
94
0
  zlib_post_call(strm, status);
95
0
  if (status == Z_OK)
96
0
    return;
97
0
  die("inflateInit: %s (%s)", zerr_to_string(status),
98
0
      strm->z.msg ? strm->z.msg : "no message");
99
0
}
100
101
void git_inflate_init_gzip_only(git_zstream *strm)
102
0
{
103
  /*
104
   * Use default 15 bits, +16 is to accept only gzip and to
105
   * yield Z_DATA_ERROR when fed zlib format.
106
   */
107
0
  const int windowBits = 15 + 16;
108
0
  int status;
109
110
0
  zlib_pre_call(strm);
111
0
  status = inflateInit2(&strm->z, windowBits);
112
0
  zlib_post_call(strm, status);
113
0
  if (status == Z_OK)
114
0
    return;
115
0
  die("inflateInit2: %s (%s)", zerr_to_string(status),
116
0
      strm->z.msg ? strm->z.msg : "no message");
117
0
}
118
119
void git_inflate_end(git_zstream *strm)
120
0
{
121
0
  int status;
122
123
0
  zlib_pre_call(strm);
124
0
  status = inflateEnd(&strm->z);
125
0
  zlib_post_call(strm, status);
126
0
  if (status == Z_OK)
127
0
    return;
128
0
  error("inflateEnd: %s (%s)", zerr_to_string(status),
129
0
        strm->z.msg ? strm->z.msg : "no message");
130
0
}
131
132
int git_inflate(git_zstream *strm, int flush)
133
0
{
134
0
  int status;
135
136
0
  for (;;) {
137
0
    zlib_pre_call(strm);
138
    /* Never say Z_FINISH unless we are feeding everything */
139
0
    status = inflate(&strm->z,
140
0
         (strm->z.avail_in != strm->avail_in)
141
0
         ? 0 : flush);
142
0
    if (status == Z_MEM_ERROR)
143
0
      die("inflate: out of memory");
144
0
    zlib_post_call(strm, status);
145
146
    /*
147
     * Let zlib work another round, while we can still
148
     * make progress.
149
     */
150
0
    if ((strm->avail_out && !strm->z.avail_out) &&
151
0
        (status == Z_OK || status == Z_BUF_ERROR))
152
0
      continue;
153
0
    break;
154
0
  }
155
156
0
  switch (status) {
157
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
158
0
  case Z_BUF_ERROR:
159
0
  case Z_OK:
160
0
  case Z_STREAM_END:
161
0
    return status;
162
0
  default:
163
0
    break;
164
0
  }
165
0
  error("inflate: %s (%s)", zerr_to_string(status),
166
0
        strm->z.msg ? strm->z.msg : "no message");
167
0
  return status;
168
0
}
169
170
size_t git_deflate_bound(git_zstream *strm, size_t size)
171
0
{
172
#if SIZE_MAX > ULONG_MAX
173
  if (size > maximum_unsigned_value_of_type(uLong))
174
    /*
175
     * deflateBound() takes uLong, which is 32-bit on
176
     * Windows. For inputs above that range, return zlib's
177
     * stored-block formula (the conservative path it would
178
     * itself use for an unknown stream state) plus the
179
     * worst-case wrapper overhead.
180
     */
181
    return size + (size >> 5) + (size >> 7) + (size >> 11)
182
      + 7 + 18;
183
#endif
184
0
  return deflateBound(&strm->z, (uLong)size);
185
0
}
186
187
void git_deflate_init(git_zstream *strm, int level)
188
0
{
189
0
  int status;
190
191
0
  memset(strm, 0, sizeof(*strm));
192
0
  zlib_pre_call(strm);
193
0
  status = deflateInit(&strm->z, level);
194
0
  zlib_post_call(strm, status);
195
0
  if (status == Z_OK)
196
0
    return;
197
0
  die("deflateInit: %s (%s)", zerr_to_string(status),
198
0
      strm->z.msg ? strm->z.msg : "no message");
199
0
}
200
201
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
202
0
{
203
0
  int status;
204
205
0
  memset(strm, 0, sizeof(*strm));
206
0
  zlib_pre_call(strm);
207
0
  status = deflateInit2(&strm->z, level,
208
0
          Z_DEFLATED, windowBits,
209
0
          8, Z_DEFAULT_STRATEGY);
210
0
  zlib_post_call(strm, status);
211
0
  if (status == Z_OK)
212
0
    return;
213
0
  die("deflateInit2: %s (%s)", zerr_to_string(status),
214
0
      strm->z.msg ? strm->z.msg : "no message");
215
0
}
216
217
void git_deflate_init_gzip(git_zstream *strm, int level)
218
0
{
219
  /*
220
   * Use default 15 bits, +16 is to generate gzip header/trailer
221
   * instead of the zlib wrapper.
222
   */
223
0
  do_git_deflate_init(strm, level, 15 + 16);
224
0
}
225
226
void git_deflate_init_raw(git_zstream *strm, int level)
227
0
{
228
  /*
229
   * Use default 15 bits, negate the value to get raw compressed
230
   * data without zlib header and trailer.
231
   */
232
0
  do_git_deflate_init(strm, level, -15);
233
0
}
234
235
int git_deflate_abort(git_zstream *strm)
236
0
{
237
0
  int status;
238
239
0
  zlib_pre_call(strm);
240
0
  status = deflateEnd(&strm->z);
241
0
  zlib_post_call(strm, status);
242
0
  return status;
243
0
}
244
245
void git_deflate_end(git_zstream *strm)
246
0
{
247
0
  int status = git_deflate_abort(strm);
248
249
0
  if (status == Z_OK)
250
0
    return;
251
0
  error("deflateEnd: %s (%s)", zerr_to_string(status),
252
0
        strm->z.msg ? strm->z.msg : "no message");
253
0
}
254
255
int git_deflate_end_gently(git_zstream *strm)
256
0
{
257
0
  int status;
258
259
0
  zlib_pre_call(strm);
260
0
  status = deflateEnd(&strm->z);
261
0
  zlib_post_call(strm, status);
262
0
  return status;
263
0
}
264
265
int git_deflate(git_zstream *strm, int flush)
266
0
{
267
0
  int status;
268
269
0
  for (;;) {
270
0
    zlib_pre_call(strm);
271
272
    /* Never say Z_FINISH unless we are feeding everything */
273
0
    status = deflate(&strm->z,
274
0
         (strm->z.avail_in != strm->avail_in)
275
0
         ? 0 : flush);
276
0
    if (status == Z_MEM_ERROR)
277
0
      die("deflate: out of memory");
278
0
    zlib_post_call(strm, status);
279
280
    /*
281
     * Let zlib work another round, while we can still
282
     * make progress.
283
     */
284
0
    if ((strm->avail_out && !strm->z.avail_out) &&
285
0
        (status == Z_OK || status == Z_BUF_ERROR))
286
0
      continue;
287
0
    break;
288
0
  }
289
290
0
  switch (status) {
291
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
292
0
  case Z_BUF_ERROR:
293
0
  case Z_OK:
294
0
  case Z_STREAM_END:
295
0
    return status;
296
0
  default:
297
0
    break;
298
0
  }
299
0
  error("deflate: %s (%s)", zerr_to_string(status),
300
0
        strm->z.msg ? strm->z.msg : "no message");
301
0
  return status;
302
0
}