diff options
author | John Southworth <john.southworth@vyatta.com> | 2012-06-03 10:29:41 -0700 |
---|---|---|
committer | John Southworth <john.southworth@vyatta.com> | 2012-06-03 10:29:41 -0700 |
commit | 4610ade056855f0e03457857d0f704c6d5b689f2 (patch) | |
tree | 32e5e8afca43ac814d7d087fb7d1161340375fdd /src/cparse | |
parent | 6c4cf990bf4ea521f602a6d4e8ac4d51de461da9 (diff) | |
download | vyatta-cfg-4610ade056855f0e03457857d0f704c6d5b689f2.tar.gz vyatta-cfg-4610ade056855f0e03457857d0f704c6d5b689f2.zip |
Fix more load/commit memory leaks
This fixes some more of the memory leaks in load and commit.
Some still exist in the legacy cli_new/cli_parse code. Since that code
is supposed to be rewritten such that it interfaces with the rewritten
commit code in a clean way, these will not be fixed right now.
Diffstat (limited to 'src/cparse')
-rw-r--r-- | src/cparse/cparse.ypp | 56 | ||||
-rw-r--r-- | src/cparse/cparse_lex.l | 14 |
2 files changed, 60 insertions, 10 deletions
diff --git a/src/cparse/cparse.ypp b/src/cparse/cparse.ypp index e70f09a..d8d8adc 100644 --- a/src/cparse/cparse.ypp +++ b/src/cparse/cparse.ypp @@ -49,6 +49,29 @@ static vector<CfgNode *> cur_path; static Cpath pcomps; static vector<bool> pcomp_is_value; +static void +cparse_init() +{ + cstore_ = NULL; + ndeact = 0; + ncomment = NULL; + nname = NULL; + nval = NULL; + node_map.clear(); + pcomps.clear(); + pcomp_is_value.clear(); + cur_path.clear(); + cur_node = NULL; +} + +static void +cparse_cleanup() +{ + delete cur_parent; + delete cur_node; + cparse_init(); +} + static void add_node() { @@ -100,6 +123,15 @@ add_node() } static void +cleanup_node() +{ + free(nval); + free(nname); + free(ncomment); + nval = ncomment = nname = NULL; +} + +static void go_down() { cur_path.push_back(cur_parent); @@ -147,17 +179,21 @@ forest: /* empty */ tree: node { add_node(); + cleanup_node(); } | node { add_node(); } LEFTB { go_down(); + cleanup_node(); } forest comment RIGHTB { go_up(); } ; node: nodec { + if (nval) + free(nval); nval = NULL; } | nodec VALUE { @@ -166,6 +202,8 @@ node: nodec { ; nodec: NODE { + if (ncomment) + free(ncomment); ncomment = NULL; nname = $1.str; ndeact = $1.deactivated; @@ -192,25 +230,19 @@ cparse::parse_file(FILE *fin, Cstore& cs) #endif // ENABLE_PARSER_TRACE // initial state + cparse_init(); cparse_set_in(fin); cstore_ = &cs; - ndeact = 0; - ncomment = NULL; - nname = NULL; - nval = NULL; - node_map.clear(); - pcomps.clear(); - pcomp_is_value.clear(); - cur_path.clear(); - cur_node = NULL; cur_parent = new CfgNode(pcomps, nname, nval, ncomment, ndeact, cstore_); if (cparse_parse() != 0) { // parsing failed + cparse_cleanup(); return NULL; } if (cur_path.size() > 0) { // didn't return to top-level => invalid + cparse_cleanup(); return NULL; } return cur_parent; @@ -219,10 +251,14 @@ cparse::parse_file(FILE *fin, Cstore& cs) CfgNode * cparse::parse_file(const char *fname, Cstore& cs) { + CfgNode *ret; FILE *fin = fopen(fname, "r"); if (!fin) { return NULL; } - return parse_file(fin, cs); + ret = parse_file(fin, cs); + cparse_init(); + fclose(fin); + return ret; } diff --git a/src/cparse/cparse_lex.l b/src/cparse/cparse_lex.l index 61c016f..49f8d47 100644 --- a/src/cparse/cparse_lex.l +++ b/src/cparse/cparse_lex.l @@ -61,6 +61,18 @@ set_ret_str() str_ptr = str_buf; } +static void +free_str() +{ + free(str_buf); + free(out_buf); + node_deactivated = 0; + str_buf = NULL; + out_buf = NULL; + str_ptr = NULL; + str_buf_len = 0; +} + %} %% @@ -98,6 +110,7 @@ set_ret_str() ++tmp; } cparse_lval.str = strdup(tmp); + free_str(); BEGIN(INITIAL); return COMMENT; } @@ -170,6 +183,7 @@ set_ret_str() <sQStr>\" { set_ret_str(); cparse_lval.str = strdup(out_buf); + free_str(); BEGIN(sValue); return VALUE; } |