summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Southworth <john.southworth@vyatta.com>2012-06-03 10:29:41 -0700
committerJohn Southworth <john.southworth@vyatta.com>2012-06-03 10:29:41 -0700
commit4610ade056855f0e03457857d0f704c6d5b689f2 (patch)
tree32e5e8afca43ac814d7d087fb7d1161340375fdd /src
parent6c4cf990bf4ea521f602a6d4e8ac4d51de461da9 (diff)
downloadvyatta-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')
-rw-r--r--src/cnode/cnode-util.hpp6
-rw-r--r--src/cparse/cparse.ypp56
-rw-r--r--src/cparse/cparse_lex.l14
-rw-r--r--src/cstore/cstore.cpp2
4 files changed, 67 insertions, 11 deletions
diff --git a/src/cnode/cnode-util.hpp b/src/cnode/cnode-util.hpp
index aeeeb08..1b0f042 100644
--- a/src/cnode/cnode-util.hpp
+++ b/src/cnode/cnode-util.hpp
@@ -27,7 +27,11 @@ public:
typedef typename nodes_vec_type::iterator nodes_iter_type;
TreeNode() : _parent(0) {}
- virtual ~TreeNode() {}
+ virtual ~TreeNode() {
+ for ( nodes_iter_type it = _child_nodes.begin(); it != _child_nodes.end(); ++it )
+ delete * it;
+ _child_nodes.clear();
+ }
const nodes_vec_type& getChildNodes() const { return _child_nodes; }
size_t numChildNodes() const {
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;
}
diff --git a/src/cstore/cstore.cpp b/src/cstore/cstore.cpp
index aacd15a..cc8a029 100644
--- a/src/cstore/cstore.cpp
+++ b/src/cstore/cstore.cpp
@@ -1827,6 +1827,7 @@ Cstore::loadFile(const char *filename)
// get the config tree from the file
CfgNode *froot = cparse::parse_file(fin, *this);
+ fclose(fin);
if (!froot) {
output_user("Failed to parse specified config file\n");
return false;
@@ -1842,6 +1843,7 @@ Cstore::loadFile(const char *filename)
vector<Cpath> com_list;
get_cmds_diff(aroot, *froot, del_list, set_list, com_list);
+ delete froot;
// "apply" the changes to the working config
for (size_t i = 0; i < del_list.size(); i++) {
if (!deleteCfgPath(del_list[i])) {