aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-07-17 11:59:13 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-07-17 11:59:13 +0000
commit679212836a881b53382ea6bd811f38e00705d50d (patch)
treee5c349683a57c55b650eae77b1ca5ac40c94a175
parent0f683f818cf087d580ba2edf7c096897bc28b95a (diff)
downloadbusybox-w32-679212836a881b53382ea6bd811f38e00705d50d.tar.gz
busybox-w32-679212836a881b53382ea6bd811f38e00705d50d.tar.bz2
busybox-w32-679212836a881b53382ea6bd811f38e00705d50d.zip
- fix segfault in nameif with mactab file
(by fixing and shrink config parser) function old new delta config_free_data - 37 +37 config_open 43 48 +5 pack_gzip 1658 1660 +2 nameif_main 527 525 -2 SynchronizeFile 629 623 -6 make_device 1184 1176 -8 config_close 31 18 -13 config_read 431 393 -38 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/5 up/down: 44/-67) Total: -23 bytes
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/parse_config.c85
-rw-r--r--miscutils/crond.c13
-rw-r--r--networking/nameif.c8
-rw-r--r--util-linux/mdev.c10
5 files changed, 59 insertions, 59 deletions
diff --git a/include/libbb.h b/include/libbb.h
index c124b1a5e..aafdfa32c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -995,7 +995,7 @@ typedef struct parser_t {
995 char *line, *data; 995 char *line, *data;
996 int lineno; 996 int lineno;
997} parser_t; 997} parser_t;
998FILE* config_open(parser_t *parser, const char *filename) FAST_FUNC; 998parser_t* config_open(const char *filename) FAST_FUNC;
999/* TODO: add define magic to collapse ntokens/mintokens/comment into one int param */ 999/* TODO: add define magic to collapse ntokens/mintokens/comment into one int param */
1000int config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) FAST_FUNC; 1000int config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) FAST_FUNC;
1001void config_close(parser_t *parser) FAST_FUNC; 1001void config_close(parser_t *parser) FAST_FUNC;
diff --git a/libbb/parse_config.c b/libbb/parse_config.c
index f07099285..70f933fe3 100644
--- a/libbb/parse_config.c
+++ b/libbb/parse_config.c
@@ -31,44 +31,42 @@ Typical usage:
31 31
32*/ 32*/
33 33
34FILE* FAST_FUNC config_open(parser_t *parser, const char *filename) 34parser_t* FAST_FUNC config_open(const char *filename)
35{ 35{
36 // empty file configures nothing! 36 parser_t *parser = xzalloc(sizeof(parser_t));
37 /* empty file configures nothing */
37 parser->fp = fopen_or_warn(filename, "r"); 38 parser->fp = fopen_or_warn(filename, "r");
38 if (!parser->fp) 39 if (parser->fp)
39 return parser->fp; 40 return parser;
40 41 config_close (parser);
41 // init parser 42 if (ENABLE_FEATURE_CLEAN_UP)
42 parser->line = NULL; 43 free(parser);
43 parser->lineno = 0; 44 return NULL;
44
45 return parser->fp;
46} 45}
47 46
48void FAST_FUNC config_close(parser_t *parser) 47static void config_free_data(parser_t *const parser)
49{ 48{
50 free(parser->line); 49 free(parser->line);
51 free(parser->data); 50 free(parser->data);
51 parser->line = parser->data = NULL;
52}
53void FAST_FUNC config_close(parser_t *parser)
54{
55 config_free_data(parser);
52 fclose(parser->fp); 56 fclose(parser->fp);
53} 57}
54 58
55int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) 59int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char*delims,char comment)
56{ 60{
57 char *line, *q; 61 char *line, *q;
58 int token_num, len; 62 int ii;
59 int noreduce = (ntokens < 0); // do not treat subsequent delimiters as one delimiter 63 /* do not treat subsequent delimiters as one delimiter */
60 64 bool noreduce = (ntokens < 0);
61 if (ntokens < 0) 65 if (noreduce)
62 ntokens = -ntokens; 66 ntokens = -ntokens;
63 67
64 // nullify tokens
65 memset(tokens, 0, sizeof(void *) * ntokens); 68 memset(tokens, 0, sizeof(void *) * ntokens);
66 69 config_free_data(parser);
67 // free used line
68 free(parser->line);
69 parser->line = NULL;
70 free(parser->data);
71 parser->data = NULL;
72 70
73 while (1) { 71 while (1) {
74 int n; 72 int n;
@@ -82,13 +80,13 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
82 parser->lineno++; 80 parser->lineno++;
83 // handle continuations. Tito's code stolen :) 81 // handle continuations. Tito's code stolen :)
84 while (1) { 82 while (1) {
85 len = strlen(line); 83 ii = strlen(line);
86 if (!len) 84 if (!ii)
87 goto free_and_cont; 85 goto next_line;
88 if (line[len - 1] != '\\') 86 if (line[ii - 1] != '\\')
89 break; 87 break;
90 // multi-line object 88 // multi-line object
91 line[--len] = '\0'; 89 line[--ii] = '\0';
92//TODO: add xmalloc_fgetline-like iface but with appending to existing str 90//TODO: add xmalloc_fgetline-like iface but with appending to existing str
93 q = xmalloc_fgetline(parser->fp); 91 q = xmalloc_fgetline(parser->fp);
94 if (q) { 92 if (q) {
@@ -101,34 +99,35 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
101 if (comment) { 99 if (comment) {
102 q = strchrnul(line, comment); 100 q = strchrnul(line, comment);
103 *q = '\0'; 101 *q = '\0';
104 len = q - line; 102 ii = q - line;
105 } 103 }
106 // skip leading delimiters 104 // skip leading delimiters
107 n = strspn(line, delims); 105 n = strspn(line, delims);
108 if (n) { 106 if (n) {
109 len -= n; 107 ii -= n;
110 strcpy(line, line + n); 108 strcpy(line, line + n);
111 } 109 }
112 if (len) 110 if (ii)
113 break; 111 break;
114 // skip empty lines 112
115 free_and_cont: 113 next_line:
114 /* skip empty line */
116 free(line); 115 free(line);
117 } 116 }
118 117
119 // non-empty line found, parse and return 118 // non-empty line found, parse and return
120 119
121 // store line 120 // store line
122 parser->line = line = xrealloc(line, len + 1); 121 parser->line = line = xrealloc(line, ii + 1);
123 parser->data = xstrdup(line); 122 parser->data = xstrdup(line);
124 123
125 // now split line to tokens 124 // now split line to tokens
126//TODO: discard consecutive delimiters? 125//TODO: discard consecutive delimiters?
127 token_num = 0; 126 ii = 0;
128 ntokens--; // now it's max allowed token no 127 ntokens--; // now it's max allowed token no
129 while (1) { 128 while (1) {
130 // get next token 129 // get next token
131 if (token_num == ntokens) 130 if (ii == ntokens)
132 break; 131 break;
133 q = line + strcspn(line, delims); 132 q = line + strcspn(line, delims);
134 if (!*q) 133 if (!*q)
@@ -136,20 +135,20 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
136 // pin token 135 // pin token
137 *q++ = '\0'; 136 *q++ = '\0';
138 if (noreduce || *line) { 137 if (noreduce || *line) {
139 tokens[token_num++] = line; 138 tokens[ii++] = line;
140//bb_error_msg("L[%d] T[%s]", token_num, line); 139//bb_info_msg("L[%d] T[%s]\n", ii, line);
141 } 140 }
142 line = q; 141 line = q;
143 } 142 }
144 143
145 // non-empty remainder is also a token, 144 // non-empty remainder is also a token,
146 // so if ntokens <= 1, we just return the whole line 145 // so if ntokens <= 1, we just return the whole line
147 if (noreduce || *line) 146 if (noreduce || *line)
148 tokens[token_num++] = line; 147 tokens[ii++] = line;
149 148
150 if (token_num < mintokens) 149 if (ii < mintokens)
151 bb_error_msg_and_die("bad line %u: %d tokens found, %d needed", 150 bb_error_msg_and_die("bad line %u: %d tokens found, %d needed",
152 parser->lineno, token_num, mintokens); 151 parser->lineno, ii, mintokens);
153 152
154 return token_num; 153 return ii;
155} 154}
diff --git a/miscutils/crond.c b/miscutils/crond.c
index 41f151753..d8423cf4f 100644
--- a/miscutils/crond.c
+++ b/miscutils/crond.c
@@ -443,7 +443,7 @@ static void FixDayDow(CronLine *line)
443 443
444static void SynchronizeFile(const char *fileName) 444static void SynchronizeFile(const char *fileName)
445{ 445{
446 struct parser_t parser; 446 struct parser_t *parser;
447 struct stat sbuf; 447 struct stat sbuf;
448 int maxLines; 448 int maxLines;
449 char *tokens[6]; 449 char *tokens[6];
@@ -455,12 +455,13 @@ static void SynchronizeFile(const char *fileName)
455 return; 455 return;
456 456
457 DeleteFile(fileName); 457 DeleteFile(fileName);
458 if (!config_open(&parser, fileName)) 458 parser = config_open(fileName);
459 if (!parser)
459 return; 460 return;
460 461
461 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES; 462 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
462 463
463 if (fstat(fileno(parser.fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { 464 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
464 CronFile *file = xzalloc(sizeof(CronFile)); 465 CronFile *file = xzalloc(sizeof(CronFile));
465 CronLine **pline; 466 CronLine **pline;
466 int n; 467 int n;
@@ -468,11 +469,11 @@ static void SynchronizeFile(const char *fileName)
468 file->cf_User = xstrdup(fileName); 469 file->cf_User = xstrdup(fileName);
469 pline = &file->cf_LineBase; 470 pline = &file->cf_LineBase;
470 471
471 while (--maxLines && (n=config_read(&parser, tokens, 6, 0, " \t", '#')) >= 0) { 472 while (--maxLines && (n=config_read(parser, tokens, 6, 0, " \t", '#')) >= 0) {
472 CronLine *line; 473 CronLine *line;
473 474
474 if (DebugOpt) { 475 if (DebugOpt) {
475 crondlog(LVL5 "user:%s entry:%s", fileName, parser.data); 476 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
476 } 477 }
477 478
478 /* check if line is setting MAILTO= */ 479 /* check if line is setting MAILTO= */
@@ -519,7 +520,7 @@ static void SynchronizeFile(const char *fileName)
519 crondlog(WARN9 "user %s: too many lines", fileName); 520 crondlog(WARN9 "user %s: too many lines", fileName);
520 } 521 }
521 } 522 }
522 config_close(&parser); 523 config_close(parser);
523} 524}
524 525
525static void CheckUpdates(void) 526static void CheckUpdates(void)
diff --git a/networking/nameif.c b/networking/nameif.c
index c5a715e36..291780a28 100644
--- a/networking/nameif.c
+++ b/networking/nameif.c
@@ -160,12 +160,12 @@ int nameif_main(int argc, char **argv)
160 prepend_new_eth_table(&clist, ifname, *argv++); 160 prepend_new_eth_table(&clist, ifname, *argv++);
161 } 161 }
162 } else { 162 } else {
163 struct parser_t parser; 163 struct parser_t *parser = config_open(fname);
164 if (config_open(&parser, fname)) { 164 if (parser) {
165 char *tokens[2]; 165 char *tokens[2];
166 while (config_read(&parser, tokens, 2, 2, " \t", '#') >= 0) 166 while (config_read(parser, tokens, 2, 2, " \t", '#') >= 0)
167 prepend_new_eth_table(&clist, tokens[0], tokens[1]); 167 prepend_new_eth_table(&clist, tokens[0], tokens[1]);
168 config_close(&parser); 168 config_close(parser);
169 } 169 }
170 } 170 }
171 171
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index c04410c0c..f83dd6adf 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -94,14 +94,14 @@ static void make_device(char *path, int delete)
94 type = S_IFBLK; 94 type = S_IFBLK;
95 95
96 if (ENABLE_FEATURE_MDEV_CONF) { 96 if (ENABLE_FEATURE_MDEV_CONF) {
97 parser_t parser; 97 parser_t *parser = config_open("/etc/mdev.conf");
98 char *tokens[5]; 98 char *tokens[5];
99 99
100 /* If we have config file, look up user settings */ 100 /* If we have config file, look up user settings */
101 if (!config_open(&parser, "/etc/mdev.conf")) 101 if (!parser)
102 goto end_parse; 102 goto end_parse;
103 103
104 while (config_read(&parser, tokens, 4, 3, " \t", '#') >= 0) { 104 while (config_read(parser, tokens, 4, 3, " \t", '#') >= 0) {
105 regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; 105 regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
106 char *val; 106 char *val;
107 107
@@ -213,7 +213,7 @@ static void make_device(char *path, int delete)
213 const char *s2 = strchr(s, *val); 213 const char *s2 = strchr(s, *val);
214 214
215 if (!s2) 215 if (!s2)
216 bb_error_msg_and_die("bad line %u", parser.lineno); 216 bb_error_msg_and_die("bad line %u", parser->lineno);
217 217
218 /* Correlate the position in the "@$*" with the delete 218 /* Correlate the position in the "@$*" with the delete
219 * step so that we get the proper behavior: 219 * step so that we get the proper behavior:
@@ -229,7 +229,7 @@ static void make_device(char *path, int delete)
229 break; /* we found matching line, stop */ 229 break; /* we found matching line, stop */
230 } /* end of "while line is read from /etc/mdev.conf" */ 230 } /* end of "while line is read from /etc/mdev.conf" */
231 231
232 config_close(&parser); 232 config_close(parser);
233 } 233 }
234 end_parse: 234 end_parse:
235 235