0001-Fix-zlib-lzma-decompression.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.
  5. Let {zlib,lzma}_decompress_file() return NULL if anything wrong happened
  6. to allow the other method to have a chance to run.
  7. Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
  8. Signed-off-by: Simon Horman <horms@verge.net.au>
  9. ---
  10. kexec/lzma.c | 33 ++++++++++++++++++++++-----------
  11. kexec/zlib.c | 57 +++++++++++++++++++++++++++++++++++----------------------
  12. 2 files changed, 57 insertions(+), 33 deletions(-)
  13. diff --git a/kexec/lzma.c b/kexec/lzma.c
  14. index 939aeb3..5bfccb7 100644
  15. --- a/kexec/lzma.c
  16. +++ b/kexec/lzma.c
  17. @@ -162,13 +162,16 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
  18. off_t size, allocated;
  19. ssize_t result;
  20. - if (!filename) {
  21. - *r_size = 0;
  22. - return 0;
  23. - }
  24. + dbgprintf("Try LZMA decompression.\n");
  25. +
  26. + *r_size = 0;
  27. + if (!filename)
  28. + return NULL;
  29. +
  30. fp = lzopen(filename, "rb");
  31. if (fp == 0) {
  32. - die("Cannot open `%s'\n", filename);
  33. + dbgprintf("Cannot open `%s'\n", filename);
  34. + return NULL;
  35. }
  36. size = 0;
  37. allocated = 65536;
  38. @@ -183,17 +186,25 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
  39. if ((errno == EINTR) || (errno == EAGAIN))
  40. continue;
  41. - die ("read on %s of %ld bytes failed\n",
  42. - filename, (allocated - size) + 0UL);
  43. + dbgprintf("%s: read on %s of %ld bytes failed\n",
  44. + __func__, filename, (allocated - size) + 0UL);
  45. + break;
  46. }
  47. size += result;
  48. - } while(result > 0);
  49. - result = lzclose(fp);
  50. - if (result != LZMA_OK) {
  51. - die ("Close of %s failed\n", filename);
  52. + } while (result > 0);
  53. +
  54. + if (lzclose(fp) != LZMA_OK) {
  55. + dbgprintf("%s: Close of %s failed\n", __func__, filename);
  56. + goto fail;
  57. }
  58. + if (result < 0)
  59. + goto fail;
  60. +
  61. *r_size = size;
  62. return buf;
  63. +fail:
  64. + free(buf);
  65. + return NULL;
  66. }
  67. #else
  68. char *lzma_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size))
  69. diff --git a/kexec/zlib.c b/kexec/zlib.c
  70. index d44df12..7170ac3 100644
  71. --- a/kexec/zlib.c
  72. +++ b/kexec/zlib.c
  73. @@ -15,29 +15,39 @@
  74. #include <ctype.h>
  75. #include <zlib.h>
  76. +static void _gzerror(gzFile fp, int *errnum, const char **errmsg)
  77. +{
  78. + *errmsg = gzerror(fp, errnum);
  79. + if (*errnum == Z_ERRNO) {
  80. + *errmsg = strerror(*errnum);
  81. + }
  82. +}
  83. +
  84. char *zlib_decompress_file(const char *filename, off_t *r_size)
  85. {
  86. gzFile fp;
  87. int errnum;
  88. const char *msg;
  89. char *buf;
  90. - off_t size, allocated;
  91. + off_t size = 0, allocated;
  92. ssize_t result;
  93. + dbgprintf("Try gzip decompression.\n");
  94. +
  95. + *r_size = 0;
  96. if (!filename) {
  97. - *r_size = 0;
  98. - return 0;
  99. + return NULL;
  100. }
  101. fp = gzopen(filename, "rb");
  102. if (fp == 0) {
  103. - msg = gzerror(fp, &errnum);
  104. - if (errnum == Z_ERRNO) {
  105. - msg = strerror(errno);
  106. - }
  107. - fprintf(stderr, "Cannot open `%s': %s\n", filename, msg);
  108. + _gzerror(fp, &errnum, &msg);
  109. + dbgprintf("Cannot open `%s': %s\n", filename, msg);
  110. + return NULL;
  111. + }
  112. + if (gzdirect(fp)) {
  113. + /* It's not in gzip format */
  114. return NULL;
  115. }
  116. - size = 0;
  117. allocated = 65536;
  118. buf = xmalloc(allocated);
  119. do {
  120. @@ -49,25 +59,28 @@ char *zlib_decompress_file(const char *filename, off_t *r_size)
  121. if (result < 0) {
  122. if ((errno == EINTR) || (errno == EAGAIN))
  123. continue;
  124. -
  125. - msg = gzerror(fp, &errnum);
  126. - if (errnum == Z_ERRNO) {
  127. - msg = strerror(errno);
  128. - }
  129. - die ("read on %s of %ld bytes failed: %s\n",
  130. - filename, (allocated - size) + 0UL, msg);
  131. + _gzerror(fp, &errnum, &msg);
  132. + dbgprintf("Read on %s of %ld bytes failed: %s\n",
  133. + filename, (allocated - size) + 0UL, msg);
  134. + size = 0;
  135. + goto fail;
  136. }
  137. size += result;
  138. } while(result > 0);
  139. +
  140. +fail:
  141. result = gzclose(fp);
  142. if (result != Z_OK) {
  143. - msg = gzerror(fp, &errnum);
  144. - if (errnum == Z_ERRNO) {
  145. - msg = strerror(errno);
  146. - }
  147. - die ("Close of %s failed: %s\n", filename, msg);
  148. + _gzerror(fp, &errnum, &msg);
  149. + dbgprintf(" Close of %s failed: %s\n", filename, msg);
  150. + }
  151. +
  152. + if (size > 0) {
  153. + *r_size = size;
  154. + } else {
  155. + free(buf);
  156. + buf = NULL;
  157. }
  158. - *r_size = size;
  159. return buf;
  160. }
  161. #else
  162. --
  163. 1.7.10.4