improve error checking around inflateInit()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
diff --git a/lib/inflate.c b/lib/inflate.c
index 28506a8..d5a41ac 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -16,6 +16,7 @@
#include <sys/queue.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -37,14 +38,21 @@ const struct got_error *
got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize)
{
const struct got_error *err = NULL;
+ int zerr;
memset(&zb->z, 0, sizeof(zb->z));
zb->z.zalloc = Z_NULL;
zb->z.zfree = Z_NULL;
- if (inflateInit(&zb->z) != Z_OK) {
- err = got_error(GOT_ERR_IO);
- goto done;
+ zerr = inflateInit(&zb->z);
+ if (zerr != Z_OK) {
+ if (zerr == Z_ERRNO)
+ return got_error_from_errno();
+ if (zerr == Z_MEM_ERROR) {
+ errno = ENOMEM;
+ return got_error_from_errno();
+ }
+ return got_error(GOT_ERR_DECOMPRESSION);
}
zb->inlen = zb->outlen = bufsize;