Fix some stupid parser bugs, most notably forgetting to allocate space for \0.

This commit is contained in:
Nicholas Marriott 2008-06-16 20:25:54 +00:00
parent 55d5b83408
commit 21a9b3f6e8

34
cfg.c
View File

@ -1,4 +1,4 @@
/* $Id: cfg.c,v 1.8 2008-06-16 17:35:40 nicm Exp $ */ /* $Id: cfg.c,v 1.9 2008-06-16 20:25:54 nicm Exp $ */
/* /*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@ -79,13 +79,13 @@ load_cfg(const char *path, char **causep)
case '\'': case '\'':
if ((s = cfg_string(f, '\'', 0)) == NULL) if ((s = cfg_string(f, '\'', 0)) == NULL)
goto error; goto error;
argv = xrealloc(argv, argc + 1, sizeof (char *)); argv = xrealloc(argv, argc + 1, sizeof *argv);
argv[argc++] = s; argv[argc++] = s;
break; break;
case '"': case '"':
if ((s = cfg_string(f, '"', 1)) == NULL) if ((s = cfg_string(f, '"', 1)) == NULL)
goto error; goto error;
argv = xrealloc(argv, argc + 1, sizeof (char *)); argv = xrealloc(argv, argc + 1, sizeof *argv);
argv[argc++] = s; argv[argc++] = s;
break; break;
case '#': case '#':
@ -97,11 +97,11 @@ load_cfg(const char *path, char **causep)
case EOF: case EOF:
case ' ': case ' ':
case '\t': case '\t':
if (len != 0) { if (len != 0) {
buf = xrealloc(buf, 1, len + 1);
buf[len] = '\0'; buf[len] = '\0';
argv = xrealloc( argv = xrealloc(argv, argc + 1, sizeof *argv);
argv, argc + 1, sizeof (char *));
argv[argc++] = buf; argv[argc++] = buf;
buf = NULL; buf = NULL;
@ -115,7 +115,7 @@ load_cfg(const char *path, char **causep)
break; break;
if ((cmd = cmd_parse(argc, argv, &cause)) == NULL) if ((cmd = cmd_parse(argc, argv, &cause)) == NULL)
goto error; goto error;
ctx.msgdata = NULL; ctx.msgdata = NULL;
ctx.cursession = NULL; ctx.cursession = NULL;
@ -176,21 +176,19 @@ cfg_string(FILE *f, char endch, int esc)
char *buf; char *buf;
size_t len; size_t len;
buf = xmalloc(1); buf = NULL;
len = 0; len = 0;
while ((ch = getc(f)) != endch) { while ((ch = getc(f)) != endch) {
switch (ch) { switch (ch) {
case EOF: case EOF:
xfree(buf); goto error;
return (NULL);
case '\\': case '\\':
if (!esc) if (!esc)
break; break;
switch (ch = getc(f)) { switch (ch = getc(f)) {
case EOF: case EOF:
xfree(buf); goto error;
return (NULL);
case 'r': case 'r':
ch = '\r'; ch = '\r';
break; break;
@ -204,14 +202,18 @@ cfg_string(FILE *f, char endch, int esc)
break; break;
} }
if (len >= SIZE_MAX - 2) { if (len >= SIZE_MAX - 2)
xfree(buf); goto error;
return (NULL);
}
buf = xrealloc(buf, 1, len + 1); buf = xrealloc(buf, 1, len + 1);
buf[len++] = ch; buf[len++] = ch;
} }
buf = xrealloc(buf, 1, len + 1);
buf[len] = '\0'; buf[len] = '\0';
return (buf); return (buf);
error:
if (buf != NULL)
xfree(buf);
return (NULL);
} }