Coverage Report

Created: 2026-02-14 06:27

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
static inline uInt zlib_buf_cap(unsigned long len)
34
0
{
35
0
  return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
36
0
}
37
38
static void zlib_pre_call(git_zstream *s)
39
0
{
40
0
  s->z.next_in = s->next_in;
41
0
  s->z.next_out = s->next_out;
42
0
  s->z.total_in = s->total_in;
43
0
  s->z.total_out = s->total_out;
44
0
  s->z.avail_in = zlib_buf_cap(s->avail_in);
45
0
  s->z.avail_out = zlib_buf_cap(s->avail_out);
46
0
}
47
48
static void zlib_post_call(git_zstream *s, int status)
49
0
{
50
0
  unsigned long bytes_consumed;
51
0
  unsigned long bytes_produced;
52
53
0
  bytes_consumed = s->z.next_in - s->next_in;
54
0
  bytes_produced = s->z.next_out - s->next_out;
55
0
  if (s->z.total_out != s->total_out + bytes_produced)
56
0
    BUG("total_out mismatch");
57
  /*
58
   * zlib does not update total_in when it returns Z_NEED_DICT,
59
   * causing a mismatch here. Skip the sanity check in that case.
60
   */
61
0
  if (status != Z_NEED_DICT &&
62
0
      s->z.total_in != s->total_in + bytes_consumed)
63
0
    BUG("total_in mismatch");
64
65
0
  s->total_out = s->z.total_out;
66
0
  s->total_in = s->z.total_in;
67
  /* zlib-ng marks `next_in` as `const`, so we have to cast it away. */
68
0
  s->next_in = (unsigned char *) s->z.next_in;
69
0
  s->next_out = s->z.next_out;
70
0
  s->avail_in -= bytes_consumed;
71
0
  s->avail_out -= bytes_produced;
72
0
}
73
74
void git_inflate_init(git_zstream *strm)
75
0
{
76
0
  int status;
77
78
0
  zlib_pre_call(strm);
79
0
  status = inflateInit(&strm->z);
80
0
  zlib_post_call(strm, status);
81
0
  if (status == Z_OK)
82
0
    return;
83
0
  die("inflateInit: %s (%s)", zerr_to_string(status),
84
0
      strm->z.msg ? strm->z.msg : "no message");
85
0
}
86
87
void git_inflate_init_gzip_only(git_zstream *strm)
88
0
{
89
  /*
90
   * Use default 15 bits, +16 is to accept only gzip and to
91
   * yield Z_DATA_ERROR when fed zlib format.
92
   */
93
0
  const int windowBits = 15 + 16;
94
0
  int status;
95
96
0
  zlib_pre_call(strm);
97
0
  status = inflateInit2(&strm->z, windowBits);
98
0
  zlib_post_call(strm, status);
99
0
  if (status == Z_OK)
100
0
    return;
101
0
  die("inflateInit2: %s (%s)", zerr_to_string(status),
102
0
      strm->z.msg ? strm->z.msg : "no message");
103
0
}
104
105
void git_inflate_end(git_zstream *strm)
106
0
{
107
0
  int status;
108
109
0
  zlib_pre_call(strm);
110
0
  status = inflateEnd(&strm->z);
111
0
  zlib_post_call(strm, status);
112
0
  if (status == Z_OK)
113
0
    return;
114
0
  error("inflateEnd: %s (%s)", zerr_to_string(status),
115
0
        strm->z.msg ? strm->z.msg : "no message");
116
0
}
117
118
int git_inflate(git_zstream *strm, int flush)
119
0
{
120
0
  int status;
121
122
0
  for (;;) {
123
0
    zlib_pre_call(strm);
124
    /* Never say Z_FINISH unless we are feeding everything */
125
0
    status = inflate(&strm->z,
126
0
         (strm->z.avail_in != strm->avail_in)
127
0
         ? 0 : flush);
128
0
    if (status == Z_MEM_ERROR)
129
0
      die("inflate: out of memory");
130
0
    zlib_post_call(strm, status);
131
132
    /*
133
     * Let zlib work another round, while we can still
134
     * make progress.
135
     */
136
0
    if ((strm->avail_out && !strm->z.avail_out) &&
137
0
        (status == Z_OK || status == Z_BUF_ERROR))
138
0
      continue;
139
0
    break;
140
0
  }
141
142
0
  switch (status) {
143
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
144
0
  case Z_BUF_ERROR:
145
0
  case Z_OK:
146
0
  case Z_STREAM_END:
147
0
    return status;
148
0
  default:
149
0
    break;
150
0
  }
151
0
  error("inflate: %s (%s)", zerr_to_string(status),
152
0
        strm->z.msg ? strm->z.msg : "no message");
153
0
  return status;
154
0
}
155
156
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
157
0
{
158
0
  return deflateBound(&strm->z, size);
159
0
}
160
161
void git_deflate_init(git_zstream *strm, int level)
162
0
{
163
0
  int status;
164
165
0
  memset(strm, 0, sizeof(*strm));
166
0
  zlib_pre_call(strm);
167
0
  status = deflateInit(&strm->z, level);
168
0
  zlib_post_call(strm, status);
169
0
  if (status == Z_OK)
170
0
    return;
171
0
  die("deflateInit: %s (%s)", zerr_to_string(status),
172
0
      strm->z.msg ? strm->z.msg : "no message");
173
0
}
174
175
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
176
0
{
177
0
  int status;
178
179
0
  memset(strm, 0, sizeof(*strm));
180
0
  zlib_pre_call(strm);
181
0
  status = deflateInit2(&strm->z, level,
182
0
          Z_DEFLATED, windowBits,
183
0
          8, Z_DEFAULT_STRATEGY);
184
0
  zlib_post_call(strm, status);
185
0
  if (status == Z_OK)
186
0
    return;
187
0
  die("deflateInit2: %s (%s)", zerr_to_string(status),
188
0
      strm->z.msg ? strm->z.msg : "no message");
189
0
}
190
191
void git_deflate_init_gzip(git_zstream *strm, int level)
192
0
{
193
  /*
194
   * Use default 15 bits, +16 is to generate gzip header/trailer
195
   * instead of the zlib wrapper.
196
   */
197
0
  do_git_deflate_init(strm, level, 15 + 16);
198
0
}
199
200
void git_deflate_init_raw(git_zstream *strm, int level)
201
0
{
202
  /*
203
   * Use default 15 bits, negate the value to get raw compressed
204
   * data without zlib header and trailer.
205
   */
206
0
  do_git_deflate_init(strm, level, -15);
207
0
}
208
209
int git_deflate_abort(git_zstream *strm)
210
0
{
211
0
  int status;
212
213
0
  zlib_pre_call(strm);
214
0
  status = deflateEnd(&strm->z);
215
0
  zlib_post_call(strm, status);
216
0
  return status;
217
0
}
218
219
void git_deflate_end(git_zstream *strm)
220
0
{
221
0
  int status = git_deflate_abort(strm);
222
223
0
  if (status == Z_OK)
224
0
    return;
225
0
  error("deflateEnd: %s (%s)", zerr_to_string(status),
226
0
        strm->z.msg ? strm->z.msg : "no message");
227
0
}
228
229
int git_deflate_end_gently(git_zstream *strm)
230
0
{
231
0
  int status;
232
233
0
  zlib_pre_call(strm);
234
0
  status = deflateEnd(&strm->z);
235
0
  zlib_post_call(strm, status);
236
0
  return status;
237
0
}
238
239
int git_deflate(git_zstream *strm, int flush)
240
0
{
241
0
  int status;
242
243
0
  for (;;) {
244
0
    zlib_pre_call(strm);
245
246
    /* Never say Z_FINISH unless we are feeding everything */
247
0
    status = deflate(&strm->z,
248
0
         (strm->z.avail_in != strm->avail_in)
249
0
         ? 0 : flush);
250
0
    if (status == Z_MEM_ERROR)
251
0
      die("deflate: out of memory");
252
0
    zlib_post_call(strm, status);
253
254
    /*
255
     * Let zlib work another round, while we can still
256
     * make progress.
257
     */
258
0
    if ((strm->avail_out && !strm->z.avail_out) &&
259
0
        (status == Z_OK || status == Z_BUF_ERROR))
260
0
      continue;
261
0
    break;
262
0
  }
263
264
0
  switch (status) {
265
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
266
0
  case Z_BUF_ERROR:
267
0
  case Z_OK:
268
0
  case Z_STREAM_END:
269
0
    return status;
270
0
  default:
271
0
    break;
272
0
  }
273
0
  error("deflate: %s (%s)", zerr_to_string(status),
274
0
        strm->z.msg ? strm->z.msg : "no message");
275
0
  return status;
276
0
}