1 From d606837b56d46eb7f815b5d85f07fcc3f1555d00 Mon Sep 17 00:00:00 2001
2 From: Yousong Zhou <yszhou4tech@gmail.com>
3 Date: Sun, 1 Feb 2015 00:10:07 +0800
4 Subject: [PATCH 1/5] Fix zlib/lzma decompression.
6 Let {zlib,lzma}_decompress_file() return NULL if anything wrong happened
7 to allow the other method to have a chance to run.
9 Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
10 Signed-off-by: Simon Horman <horms@verge.net.au>
12 kexec/lzma.c | 33 ++++++++++++++++++++++-----------
13 kexec/zlib.c | 57 +++++++++++++++++++++++++++++++++++----------------------
14 2 files changed, 57 insertions(+), 33 deletions(-)
16 diff --git a/kexec/lzma.c b/kexec/lzma.c
17 index 939aeb3..5bfccb7 100644
20 @@ -162,13 +162,16 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
21 off_t size, allocated;
28 + dbgprintf("Try LZMA decompression.\n");
34 fp = lzopen(filename, "rb");
36 - die("Cannot open `%s'\n", filename);
37 + dbgprintf("Cannot open `%s'\n", filename);
42 @@ -183,17 +186,25 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
43 if ((errno == EINTR) || (errno == EAGAIN))
46 - die ("read on %s of %ld bytes failed\n",
47 - filename, (allocated - size) + 0UL);
48 + dbgprintf("%s: read on %s of %ld bytes failed\n",
49 + __func__, filename, (allocated - size) + 0UL);
53 - } while(result > 0);
54 - result = lzclose(fp);
55 - if (result != LZMA_OK) {
56 - die ("Close of %s failed\n", filename);
57 + } while (result > 0);
59 + if (lzclose(fp) != LZMA_OK) {
60 + dbgprintf("%s: Close of %s failed\n", __func__, filename);
73 char *lzma_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size))
74 diff --git a/kexec/zlib.c b/kexec/zlib.c
75 index d44df12..7170ac3 100644
82 +static void _gzerror(gzFile fp, int *errnum, const char **errmsg)
84 + *errmsg = gzerror(fp, errnum);
85 + if (*errnum == Z_ERRNO) {
86 + *errmsg = strerror(*errnum);
90 char *zlib_decompress_file(const char *filename, off_t *r_size)
96 - off_t size, allocated;
97 + off_t size = 0, allocated;
100 + dbgprintf("Try gzip decompression.\n");
108 fp = gzopen(filename, "rb");
110 - msg = gzerror(fp, &errnum);
111 - if (errnum == Z_ERRNO) {
112 - msg = strerror(errno);
114 - fprintf(stderr, "Cannot open `%s': %s\n", filename, msg);
115 + _gzerror(fp, &errnum, &msg);
116 + dbgprintf("Cannot open `%s': %s\n", filename, msg);
119 + if (gzdirect(fp)) {
120 + /* It's not in gzip format */
125 buf = xmalloc(allocated);
127 @@ -49,25 +59,28 @@ char *zlib_decompress_file(const char *filename, off_t *r_size)
129 if ((errno == EINTR) || (errno == EAGAIN))
132 - msg = gzerror(fp, &errnum);
133 - if (errnum == Z_ERRNO) {
134 - msg = strerror(errno);
136 - die ("read on %s of %ld bytes failed: %s\n",
137 - filename, (allocated - size) + 0UL, msg);
138 + _gzerror(fp, &errnum, &msg);
139 + dbgprintf("Read on %s of %ld bytes failed: %s\n",
140 + filename, (allocated - size) + 0UL, msg);
148 result = gzclose(fp);
149 if (result != Z_OK) {
150 - msg = gzerror(fp, &errnum);
151 - if (errnum == Z_ERRNO) {
152 - msg = strerror(errno);
154 - die ("Close of %s failed: %s\n", filename, msg);
155 + _gzerror(fp, &errnum, &msg);
156 + dbgprintf(" Close of %s failed: %s\n", filename, msg);