fix for a double free bug
[project/uci.git] / parse.c
diff --git a/parse.c b/parse.c
index 4c51715..5158028 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -56,6 +56,7 @@ static void uci_getln(struct uci_context *ctx)
                }
 
                if (pctx->bufsz > LINEBUF_MAX/2) {
+                       pctx->reason = "line too long";
                        pctx->byte = LINEBUF_MAX;
                        UCI_THROW(ctx, UCI_ERR_PARSE);
                }
@@ -76,6 +77,7 @@ static void uci_parse_cleanup(struct uci_context *ctx)
        if (!pctx)
                return;
 
+       ctx->pctx = NULL;
        if (pctx->cfg) {
                uci_list_del(&pctx->cfg->list);
                uci_drop_file(pctx->cfg);
@@ -137,6 +139,7 @@ static void parse_double_quote(struct uci_context *ctx, char **str, char **targe
                        break;
                }
        }
+       ctx->pctx->reason = "unterminated \"";
        ctx->pctx->byte = *str - ctx->pctx->buf;
        UCI_THROW(ctx, UCI_ERR_PARSE);
 }
@@ -160,6 +163,7 @@ static void parse_single_quote(struct uci_context *ctx, char **str, char **targe
                        addc(target, str);
                }
        }
+       ctx->pctx->reason = "unterminated '";
        ctx->pctx->byte = *str - ctx->pctx->buf;
        UCI_THROW(ctx, UCI_ERR_PARSE);
 }
@@ -213,6 +217,7 @@ static char *next_arg(struct uci_context *ctx, char **str, bool required)
        skip_whitespace(str);
        parse_str(ctx, str, &ptr);
        if (required && !*val) {
+               ctx->pctx->reason = "insufficient arguments";
                ctx->pctx->byte = *str - ctx->pctx->buf;
                UCI_THROW(ctx, UCI_ERR_PARSE);
        }
@@ -230,6 +235,7 @@ static void assert_eol(struct uci_context *ctx, char **str)
 
        tmp = next_arg(ctx, str, false);
        if (tmp && *tmp) {
+               ctx->pctx->reason = "too many arguments";
                ctx->pctx->byte = tmp - ctx->pctx->buf;
                UCI_THROW(ctx, UCI_ERR_PARSE);
        }
@@ -242,13 +248,9 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
 {
        char *type, *name;
 
+       /* command string null-terminated by strtok */
        *str += strlen(*str) + 1;
 
-       if (!*str) {
-               ctx->pctx->byte = *str - ctx->pctx->buf;
-               UCI_THROW(ctx, UCI_ERR_PARSE);
-       }
-
        type = next_arg(ctx, str, true);
        name = next_arg(ctx, str, false);
        assert_eol(ctx, str);
@@ -261,13 +263,12 @@ static void uci_parse_option(struct uci_context *ctx, char **str)
 {
        char *name, *value;
 
+       /* command string null-terminated by strtok */
        *str += strlen(*str) + 1;
 
        name = next_arg(ctx, str, true);
        value = next_arg(ctx, str, true);
        assert_eol(ctx, str);
-
-       DPRINTF("\tOption: %s=\"%s\"\n", name, value);
 }
 
 /*
@@ -295,6 +296,7 @@ static void uci_parse_line(struct uci_context *ctx)
                                        uci_parse_option(ctx, &word);
                                break;
                        default:
+                               pctx->reason = "unterminated command";
                                pctx->byte = word - pctx->buf;
                                UCI_THROW(ctx, UCI_ERR_PARSE);
                                break;