aboutsummaryrefslogtreecommitdiff
path: root/libbb/parse_config.c
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 /libbb/parse_config.c
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
Diffstat (limited to 'libbb/parse_config.c')
-rw-r--r--libbb/parse_config.c85
1 files changed, 42 insertions, 43 deletions
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}