Skip to content

Commit 1a507fc

Browse files
committed
zlib wrapper: refactor error message formatter
Before refactoring the main part of the wrappers, first move the logic to convert error status that come back from zlib to string to a helper function. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5af0de commit 1a507fc

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

zlib.c

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,61 @@
44
*/
55
#include "cache.h"
66

7-
void git_inflate_init(z_streamp strm)
7+
static const char *zerr_to_string(int status)
88
{
9-
const char *err;
10-
11-
switch (inflateInit(strm)) {
12-
case Z_OK:
13-
return;
14-
9+
switch (status) {
1510
case Z_MEM_ERROR:
16-
err = "out of memory";
17-
break;
11+
return "out of memory";
1812
case Z_VERSION_ERROR:
19-
err = "wrong version";
20-
break;
13+
return "wrong version";
14+
case Z_NEED_DICT:
15+
return "needs dictionary";
16+
case Z_DATA_ERROR:
17+
return "data stream error";
18+
case Z_STREAM_ERROR:
19+
return "stream consistency error";
2120
default:
22-
err = "error";
21+
return "unknown error";
2322
}
24-
die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
2523
}
2624

27-
void git_inflate_end(z_streamp strm)
25+
void git_inflate_init(z_streamp strm)
2826
{
29-
if (inflateEnd(strm) != Z_OK)
30-
error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
27+
int status = inflateInit(strm);
28+
29+
if (status == Z_OK)
30+
return;
31+
die("inflateInit: %s (%s)", zerr_to_string(status),
32+
strm->msg ? strm->msg : "no message");
3133
}
3234

33-
int git_inflate(z_streamp strm, int flush)
35+
void git_inflate_end(z_streamp strm)
3436
{
35-
int ret = inflate(strm, flush);
36-
const char *err;
37+
int status = inflateEnd(strm);
3738

38-
switch (ret) {
39-
/* Out of memory is fatal. */
40-
case Z_MEM_ERROR:
41-
die("inflate: out of memory");
39+
if (status == Z_OK)
40+
return;
41+
error("inflateEnd: %s (%s)", zerr_to_string(status),
42+
strm->msg ? strm->msg : "no message");
43+
}
4244

43-
/* Data corruption errors: we may want to recover from them (fsck) */
44-
case Z_NEED_DICT:
45-
err = "needs dictionary"; break;
46-
case Z_DATA_ERROR:
47-
err = "data stream error"; break;
48-
case Z_STREAM_ERROR:
49-
err = "stream consistency error"; break;
50-
default:
51-
err = "unknown error"; break;
45+
int git_inflate(z_streamp strm, int flush)
46+
{
47+
int status = inflate(strm, flush);
5248

49+
switch (status) {
5350
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
5451
case Z_BUF_ERROR:
5552
case Z_OK:
5653
case Z_STREAM_END:
57-
return ret;
54+
return status;
55+
56+
case Z_MEM_ERROR:
57+
die("inflate: out of memory");
58+
default:
59+
break;
5860
}
59-
error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
60-
return ret;
61+
error("inflate: %s (%s)", zerr_to_string(status),
62+
strm->msg ? strm->msg : "no message");
63+
return status;
6164
}

0 commit comments

Comments
 (0)