1
0

020-avoid_getline.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. --- a/libopkg/parse_util.c
  2. +++ b/libopkg/parse_util.c
  3. @@ -22,6 +22,7 @@
  4. #include "libbb/libbb.h"
  5. #include "parse_util.h"
  6. +#include "pkg_parse.h"
  7. int
  8. is_field(const char *type, const char *line)
  9. @@ -86,3 +87,84 @@ parse_list(const char *raw, unsigned int
  10. *count = line_count;
  11. return depends;
  12. }
  13. +
  14. +int
  15. +parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
  16. + char **buf0, size_t buf0len)
  17. +{
  18. + int ret, lineno;
  19. + char *buf, *nl;
  20. + size_t buflen;
  21. +
  22. + lineno = 1;
  23. + ret = 0;
  24. +
  25. + buflen = buf0len;
  26. + buf = *buf0;
  27. + buf[0] = '\0';
  28. +
  29. + while (1) {
  30. + if (fgets(buf, (int)buflen, fp) == NULL) {
  31. + if (ferror(fp)) {
  32. + opkg_perror(ERROR, "fgets");
  33. + ret = -1;
  34. + } else if (strlen(*buf0) == buf0len-1) {
  35. + opkg_msg(ERROR, "Missing new line character"
  36. + " at end of file!\n");
  37. + parse_line(item, *buf0, mask);
  38. + }
  39. + break;
  40. + }
  41. +
  42. + nl = strchr(buf, '\n');
  43. + if (nl == NULL) {
  44. + if (strlen(buf) < buflen-1) {
  45. + /*
  46. + * Line could be exactly buflen-1 long and
  47. + * missing a newline, but we won't know until
  48. + * fgets fails to read more data.
  49. + */
  50. + opkg_msg(ERROR, "Missing new line character"
  51. + " at end of file!\n");
  52. + parse_line(item, *buf0, mask);
  53. + break;
  54. + }
  55. + if (buf0len >= EXCESSIVE_LINE_LEN) {
  56. + opkg_msg(ERROR, "Excessively long line at "
  57. + "%d. Corrupt file?\n",
  58. + lineno);
  59. + ret = -1;
  60. + break;
  61. + }
  62. +
  63. + /*
  64. + * Realloc and point buf past the data already read,
  65. + * at the NULL terminator inserted by fgets.
  66. + * |<--------------- buf0len ----------------->|
  67. + * | |<------- buflen ---->|
  68. + * |---------------------|---------------------|
  69. + * buf0 buf
  70. + */
  71. + buflen = buf0len +1;
  72. + buf0len *= 2;
  73. + *buf0 = xrealloc(*buf0, buf0len);
  74. + buf = *buf0 + buflen -2;
  75. +
  76. + continue;
  77. + }
  78. +
  79. + *nl = '\0';
  80. +
  81. + lineno++;
  82. +
  83. + if (parse_line(item, *buf0, mask))
  84. + break;
  85. +
  86. + buf = *buf0;
  87. + buflen = buf0len;
  88. + buf[0] = '\0';
  89. + }
  90. +
  91. + return ret;
  92. +}
  93. +
  94. --- a/libopkg/parse_util.h
  95. +++ b/libopkg/parse_util.h
  96. @@ -22,4 +22,8 @@ int is_field(const char *type, const cha
  97. char *parse_simple(const char *type, const char *line);
  98. char **parse_list(const char *raw, unsigned int *count, const char sep, int skip_field);
  99. +typedef int (*parse_line_t)(void *, const char *, uint);
  100. +int parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
  101. + char **buf0, size_t buf0len);
  102. +
  103. #endif
  104. --- a/libopkg/pkg_hash.c
  105. +++ b/libopkg/pkg_hash.c
  106. @@ -23,6 +23,7 @@
  107. #include "opkg_message.h"
  108. #include "pkg_vec.h"
  109. #include "pkg_hash.h"
  110. +#include "parse_util.h"
  111. #include "pkg_parse.h"
  112. #include "opkg_utils.h"
  113. #include "sprintf_alloc.h"
  114. @@ -119,8 +120,14 @@ pkg_hash_add_from_file(const char *file_
  115. pkg->src = src;
  116. pkg->dest = dest;
  117. - ret = pkg_parse_from_stream_nomalloc(pkg, fp, 0,
  118. + ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
  119. &buf, len);
  120. +
  121. + if (pkg->name == NULL) {
  122. + /* probably just a blank line */
  123. + ret = 1;
  124. + }
  125. +
  126. if (ret) {
  127. pkg_deinit (pkg);
  128. free(pkg);
  129. --- a/libopkg/pkg_parse.c
  130. +++ b/libopkg/pkg_parse.c
  131. @@ -104,9 +104,11 @@ get_arch_priority(const char *arch)
  132. return 0;
  133. }
  134. -static int
  135. -pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
  136. +int
  137. +pkg_parse_line(void *ptr, const char *line, uint mask)
  138. {
  139. + pkg_t *pkg = (pkg_t *) ptr;
  140. +
  141. /* these flags are a bit hackish... */
  142. static int reading_conffiles = 0, reading_description = 0;
  143. int ret = 0;
  144. @@ -266,91 +268,6 @@ dont_reset_flags:
  145. }
  146. int
  147. -pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
  148. - char **buf0, size_t buf0len)
  149. -{
  150. - int ret, lineno;
  151. - char *buf, *nl;
  152. - size_t buflen;
  153. -
  154. - lineno = 1;
  155. - ret = 0;
  156. -
  157. - buflen = buf0len;
  158. - buf = *buf0;
  159. - buf[0] = '\0';
  160. -
  161. - while (1) {
  162. - if (fgets(buf, (int)buflen, fp) == NULL) {
  163. - if (ferror(fp)) {
  164. - opkg_perror(ERROR, "fgets");
  165. - ret = -1;
  166. - } else if (strlen(*buf0) == buf0len-1) {
  167. - opkg_msg(ERROR, "Missing new line character"
  168. - " at end of file!\n");
  169. - pkg_parse_line(pkg, *buf0, mask);
  170. - }
  171. - break;
  172. - }
  173. -
  174. - nl = strchr(buf, '\n');
  175. - if (nl == NULL) {
  176. - if (strlen(buf) < buflen-1) {
  177. - /*
  178. - * Line could be exactly buflen-1 long and
  179. - * missing a newline, but we won't know until
  180. - * fgets fails to read more data.
  181. - */
  182. - opkg_msg(ERROR, "Missing new line character"
  183. - " at end of file!\n");
  184. - pkg_parse_line(pkg, *buf0, mask);
  185. - break;
  186. - }
  187. - if (buf0len >= EXCESSIVE_LINE_LEN) {
  188. - opkg_msg(ERROR, "Excessively long line at "
  189. - "%d. Corrupt file?\n",
  190. - lineno);
  191. - ret = -1;
  192. - break;
  193. - }
  194. -
  195. - /*
  196. - * Realloc and point buf past the data already read,
  197. - * at the NULL terminator inserted by fgets.
  198. - * |<--------------- buf0len ----------------->|
  199. - * | |<------- buflen ---->|
  200. - * |---------------------|---------------------|
  201. - * buf0 buf
  202. - */
  203. - buflen = buf0len +1;
  204. - buf0len *= 2;
  205. - *buf0 = xrealloc(*buf0, buf0len);
  206. - buf = *buf0 + buflen -2;
  207. -
  208. - continue;
  209. - }
  210. -
  211. - *nl = '\0';
  212. -
  213. - lineno++;
  214. -
  215. - if (pkg_parse_line(pkg, *buf0, mask))
  216. - break;
  217. -
  218. - buf = *buf0;
  219. - buflen = buf0len;
  220. - buf[0] = '\0';
  221. - }
  222. -
  223. - if (pkg->name == NULL) {
  224. - /* probably just a blank line */
  225. - ret = 1;
  226. - }
  227. -
  228. - return ret;
  229. -}
  230. -
  231. -int
  232. pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
  233. {
  234. int ret;
  235. @@ -358,8 +275,13 @@ pkg_parse_from_stream(pkg_t *pkg, FILE *
  236. const size_t len = 4096;
  237. buf = xmalloc(len);
  238. - ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
  239. + ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, len);
  240. free(buf);
  241. + if (pkg->name == NULL) {
  242. + /* probably just a blank line */
  243. + ret = 1;
  244. + }
  245. +
  246. return ret;
  247. }
  248. --- a/libopkg/pkg_parse.h
  249. +++ b/libopkg/pkg_parse.h
  250. @@ -18,10 +18,11 @@
  251. #ifndef PKG_PARSE_H
  252. #define PKG_PARSE_H
  253. +#include "pkg.h"
  254. +
  255. int parse_version(pkg_t *pkg, const char *raw);
  256. int pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask);
  257. -int pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
  258. - char **buf0, size_t buf0len);
  259. +int pkg_parse_line(void *ptr, const char *line, uint mask);
  260. #define EXCESSIVE_LINE_LEN (4096 << 8)
  261. --- a/libopkg/release_parse.c
  262. +++ b/libopkg/release_parse.c
  263. @@ -23,8 +23,10 @@
  264. #include "parse_util.h"
  265. static int
  266. -release_parse_line(release_t *release, const char *line)
  267. +release_parse_line(void *ptr, const char *line, uint mask)
  268. {
  269. + release_t *release = (release_t *) ptr;
  270. +
  271. int ret = 0;
  272. unsigned int count = 0;
  273. char **list = 0;
  274. @@ -111,25 +113,14 @@ dont_reset_flags:
  275. int
  276. release_parse_from_stream(release_t *release, FILE *fp)
  277. {
  278. - int ret = 0;
  279. - char *buf = NULL;
  280. - size_t buflen, nread;
  281. -
  282. - nread = getline(&buf, &buflen, fp);
  283. - while ( nread != -1 ) {
  284. - if (buf[nread-1] == '\n') buf[nread-1] = '\0';
  285. - if (release_parse_line(release, buf))
  286. - opkg_msg(DEBUG, "Failed to parse release line for %s:\n\t%s\n",
  287. - release->name, buf);
  288. - nread = getline(&buf, &buflen, fp);
  289. - }
  290. -
  291. - if (!feof(fp)) {
  292. - opkg_perror(ERROR, "Problems reading Release file for %sd\n", release->name);
  293. - ret = -1;
  294. - }
  295. + int ret;
  296. + char *buf;
  297. + const size_t len = 4096;
  298. + buf = xmalloc(len);
  299. + ret = parse_from_stream_nomalloc(release_parse_line, release, fp, 0, &buf, len);
  300. free(buf);
  301. +
  302. return ret;
  303. }