summaryrefslogtreecommitdiff
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-08 14:39:54 -0700
commita8b48e0dee8fcd9dbe602f8b09400d4082ca7d77 (patch)
treef3a5c67ed7ed393de0a548ce828c066dfcd89eca
parent3df95d3f999867e64d04604705f3d6f89fdad392 (diff)
downloadvyatta-cfg-a8b48e0dee8fcd9dbe602f8b09400d4082ca7d77.tar.gz
vyatta-cfg-a8b48e0dee8fcd9dbe602f8b09400d4082ca7d77.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.
-rwxr-xr-xdebian/rules1
-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
5 files changed, 68 insertions, 11 deletions
diff --git a/debian/rules b/debian/rules
index 59c7712..f2c57b7 100755
--- a/debian/rules
+++ b/debian/rules
@@ -10,6 +10,7 @@ cfg_opts += --mandir=\$${prefix}/share/man
cfg_opts += --infodir=\$${prefix}/share/info
cfg_opts += CFLAGS="$(CFLAGS)"
cfg_opts += LDFLAGS="-Wl,-z,defs"
+cfg_opts += CXXFLAGS="$(CXXFLAGS)"
inst_opts := --sourcedir=debian/tmp
clean:
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])) {