- 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
This commit is contained in:
Bernhard Reutner-Fischer 2008-07-17 11:59:13 +00:00
parent 0f683f818c
commit 679212836a
5 changed files with 59 additions and 59 deletions

View File

@ -995,7 +995,7 @@ typedef struct parser_t {
char *line, *data; char *line, *data;
int lineno; int lineno;
} parser_t; } parser_t;
FILE* config_open(parser_t *parser, const char *filename) FAST_FUNC; parser_t* config_open(const char *filename) FAST_FUNC;
/* TODO: add define magic to collapse ntokens/mintokens/comment into one int param */ /* TODO: add define magic to collapse ntokens/mintokens/comment into one int param */
int config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) FAST_FUNC; int config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) FAST_FUNC;
void config_close(parser_t *parser) FAST_FUNC; void config_close(parser_t *parser) FAST_FUNC;

View File

@ -31,44 +31,42 @@ Typical usage:
*/ */
FILE* FAST_FUNC config_open(parser_t *parser, const char *filename) parser_t* FAST_FUNC config_open(const char *filename)
{ {
// empty file configures nothing! parser_t *parser = xzalloc(sizeof(parser_t));
/* empty file configures nothing */
parser->fp = fopen_or_warn(filename, "r"); parser->fp = fopen_or_warn(filename, "r");
if (!parser->fp) if (parser->fp)
return parser->fp; return parser;
config_close (parser);
// init parser if (ENABLE_FEATURE_CLEAN_UP)
parser->line = NULL; free(parser);
parser->lineno = 0; return NULL;
return parser->fp;
} }
void FAST_FUNC config_close(parser_t *parser) static void config_free_data(parser_t *const parser)
{ {
free(parser->line); free(parser->line);
free(parser->data); free(parser->data);
parser->line = parser->data = NULL;
}
void FAST_FUNC config_close(parser_t *parser)
{
config_free_data(parser);
fclose(parser->fp); fclose(parser->fp);
} }
int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char*delims,char comment)
{ {
char *line, *q; char *line, *q;
int token_num, len; int ii;
int noreduce = (ntokens < 0); // do not treat subsequent delimiters as one delimiter /* do not treat subsequent delimiters as one delimiter */
bool noreduce = (ntokens < 0);
if (ntokens < 0) if (noreduce)
ntokens = -ntokens; ntokens = -ntokens;
// nullify tokens
memset(tokens, 0, sizeof(void *) * ntokens); memset(tokens, 0, sizeof(void *) * ntokens);
config_free_data(parser);
// free used line
free(parser->line);
parser->line = NULL;
free(parser->data);
parser->data = NULL;
while (1) { while (1) {
int n; int n;
@ -82,13 +80,13 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
parser->lineno++; parser->lineno++;
// handle continuations. Tito's code stolen :) // handle continuations. Tito's code stolen :)
while (1) { while (1) {
len = strlen(line); ii = strlen(line);
if (!len) if (!ii)
goto free_and_cont; goto next_line;
if (line[len - 1] != '\\') if (line[ii - 1] != '\\')
break; break;
// multi-line object // multi-line object
line[--len] = '\0'; line[--ii] = '\0';
//TODO: add xmalloc_fgetline-like iface but with appending to existing str //TODO: add xmalloc_fgetline-like iface but with appending to existing str
q = xmalloc_fgetline(parser->fp); q = xmalloc_fgetline(parser->fp);
if (q) { if (q) {
@ -101,34 +99,35 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
if (comment) { if (comment) {
q = strchrnul(line, comment); q = strchrnul(line, comment);
*q = '\0'; *q = '\0';
len = q - line; ii = q - line;
} }
// skip leading delimiters // skip leading delimiters
n = strspn(line, delims); n = strspn(line, delims);
if (n) { if (n) {
len -= n; ii -= n;
strcpy(line, line + n); strcpy(line, line + n);
} }
if (len) if (ii)
break; break;
// skip empty lines
free_and_cont: next_line:
/* skip empty line */
free(line); free(line);
} }
// non-empty line found, parse and return // non-empty line found, parse and return
// store line // store line
parser->line = line = xrealloc(line, len + 1); parser->line = line = xrealloc(line, ii + 1);
parser->data = xstrdup(line); parser->data = xstrdup(line);
// now split line to tokens // now split line to tokens
//TODO: discard consecutive delimiters? //TODO: discard consecutive delimiters?
token_num = 0; ii = 0;
ntokens--; // now it's max allowed token no ntokens--; // now it's max allowed token no
while (1) { while (1) {
// get next token // get next token
if (token_num == ntokens) if (ii == ntokens)
break; break;
q = line + strcspn(line, delims); q = line + strcspn(line, delims);
if (!*q) if (!*q)
@ -136,8 +135,8 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
// pin token // pin token
*q++ = '\0'; *q++ = '\0';
if (noreduce || *line) { if (noreduce || *line) {
tokens[token_num++] = line; tokens[ii++] = line;
//bb_error_msg("L[%d] T[%s]", token_num, line); //bb_info_msg("L[%d] T[%s]\n", ii, line);
} }
line = q; line = q;
} }
@ -145,11 +144,11 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mint
// non-empty remainder is also a token, // non-empty remainder is also a token,
// so if ntokens <= 1, we just return the whole line // so if ntokens <= 1, we just return the whole line
if (noreduce || *line) if (noreduce || *line)
tokens[token_num++] = line; tokens[ii++] = line;
if (token_num < mintokens) if (ii < mintokens)
bb_error_msg_and_die("bad line %u: %d tokens found, %d needed", bb_error_msg_and_die("bad line %u: %d tokens found, %d needed",
parser->lineno, token_num, mintokens); parser->lineno, ii, mintokens);
return token_num; return ii;
} }

View File

@ -443,7 +443,7 @@ static void FixDayDow(CronLine *line)
static void SynchronizeFile(const char *fileName) static void SynchronizeFile(const char *fileName)
{ {
struct parser_t parser; struct parser_t *parser;
struct stat sbuf; struct stat sbuf;
int maxLines; int maxLines;
char *tokens[6]; char *tokens[6];
@ -455,12 +455,13 @@ static void SynchronizeFile(const char *fileName)
return; return;
DeleteFile(fileName); DeleteFile(fileName);
if (!config_open(&parser, fileName)) parser = config_open(fileName);
if (!parser)
return; return;
maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES; maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
if (fstat(fileno(parser.fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
CronFile *file = xzalloc(sizeof(CronFile)); CronFile *file = xzalloc(sizeof(CronFile));
CronLine **pline; CronLine **pline;
int n; int n;
@ -468,11 +469,11 @@ static void SynchronizeFile(const char *fileName)
file->cf_User = xstrdup(fileName); file->cf_User = xstrdup(fileName);
pline = &file->cf_LineBase; pline = &file->cf_LineBase;
while (--maxLines && (n=config_read(&parser, tokens, 6, 0, " \t", '#')) >= 0) { while (--maxLines && (n=config_read(parser, tokens, 6, 0, " \t", '#')) >= 0) {
CronLine *line; CronLine *line;
if (DebugOpt) { if (DebugOpt) {
crondlog(LVL5 "user:%s entry:%s", fileName, parser.data); crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
} }
/* check if line is setting MAILTO= */ /* check if line is setting MAILTO= */
@ -519,7 +520,7 @@ static void SynchronizeFile(const char *fileName)
crondlog(WARN9 "user %s: too many lines", fileName); crondlog(WARN9 "user %s: too many lines", fileName);
} }
} }
config_close(&parser); config_close(parser);
} }
static void CheckUpdates(void) static void CheckUpdates(void)

View File

@ -160,12 +160,12 @@ int nameif_main(int argc, char **argv)
prepend_new_eth_table(&clist, ifname, *argv++); prepend_new_eth_table(&clist, ifname, *argv++);
} }
} else { } else {
struct parser_t parser; struct parser_t *parser = config_open(fname);
if (config_open(&parser, fname)) { if (parser) {
char *tokens[2]; char *tokens[2];
while (config_read(&parser, tokens, 2, 2, " \t", '#') >= 0) while (config_read(parser, tokens, 2, 2, " \t", '#') >= 0)
prepend_new_eth_table(&clist, tokens[0], tokens[1]); prepend_new_eth_table(&clist, tokens[0], tokens[1]);
config_close(&parser); config_close(parser);
} }
} }

View File

@ -94,14 +94,14 @@ static void make_device(char *path, int delete)
type = S_IFBLK; type = S_IFBLK;
if (ENABLE_FEATURE_MDEV_CONF) { if (ENABLE_FEATURE_MDEV_CONF) {
parser_t parser; parser_t *parser = config_open("/etc/mdev.conf");
char *tokens[5]; char *tokens[5];
/* If we have config file, look up user settings */ /* If we have config file, look up user settings */
if (!config_open(&parser, "/etc/mdev.conf")) if (!parser)
goto end_parse; goto end_parse;
while (config_read(&parser, tokens, 4, 3, " \t", '#') >= 0) { while (config_read(parser, tokens, 4, 3, " \t", '#') >= 0) {
regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
char *val; char *val;
@ -213,7 +213,7 @@ static void make_device(char *path, int delete)
const char *s2 = strchr(s, *val); const char *s2 = strchr(s, *val);
if (!s2) if (!s2)
bb_error_msg_and_die("bad line %u", parser.lineno); bb_error_msg_and_die("bad line %u", parser->lineno);
/* Correlate the position in the "@$*" with the delete /* Correlate the position in the "@$*" with the delete
* step so that we get the proper behavior: * step so that we get the proper behavior:
@ -229,7 +229,7 @@ static void make_device(char *path, int delete)
break; /* we found matching line, stop */ break; /* we found matching line, stop */
} /* end of "while line is read from /etc/mdev.conf" */ } /* end of "while line is read from /etc/mdev.conf" */
config_close(&parser); config_close(parser);
} }
end_parse: end_parse: