From a5c387def7981fd5b2e48759d3d1ca489cdfefcf Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Wed, 14 Aug 2013 11:10:58 +0200 Subject: src: xml: don't duplicate string in nft_table_xml_parse With this patch, nft_table_xml_parse does not duplicate the string anymore, which is what most callers seem to need. This fixes memleaks in several places in the code. Thus, this patch also adapts the code to duplicate it when needed. Based on patch from Arturo Borrero. Signed-off-by: Pablo Neira Ayuso --- src/chain.c | 3 --- src/expr/log.c | 2 +- src/mxml.c | 4 +--- src/rule.c | 4 ++-- src/set.c | 4 ++-- src/table.c | 2 +- 6 files changed, 7 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/chain.c b/src/chain.c index e2296d3..97f6d95 100644 --- a/src/chain.c +++ b/src/chain.c @@ -597,7 +597,6 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) goto err; strncpy(c->name, name, NFT_CHAIN_MAXNAMELEN); - xfree(name); c->flags |= (1 << NFT_CHAIN_ATTR_NAME); if (nft_mxml_num_parse(tree, "handle", MXML_DESCEND_FIRST, BASE_DEC, @@ -650,8 +649,6 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) goto err; hooknum = nft_str2hooknum(hooknum_str); - xfree(hooknum_str); - if (hooknum < 0) goto err; diff --git a/src/expr/log.c b/src/expr/log.c index 14785fd..291502b 100644 --- a/src/expr/log.c +++ b/src/expr/log.c @@ -162,7 +162,7 @@ static int nft_rule_expr_log_xml_parse(struct nft_rule_expr *e, mxml_node_t *tre if (prefix == NULL) return -1; - log->prefix = prefix; + log->prefix = strdup(prefix); e->flags |= (1 << NFT_EXPR_LOG_PREFIX); if (nft_mxml_num_parse(tree, "group", MXML_DESCEND_FIRST, BASE_DEC, diff --git a/src/mxml.c b/src/mxml.c index b5de153..b77936a 100644 --- a/src/mxml.c +++ b/src/mxml.c @@ -163,7 +163,7 @@ const char *nft_mxml_str_parse(mxml_node_t *tree, const char *node_name, return NULL; } - return strdup(node->child->value.opaque); + return node->child->value.opaque; } int nft_mxml_family_parse(mxml_node_t *tree, const char *node_name, @@ -177,8 +177,6 @@ int nft_mxml_family_parse(mxml_node_t *tree, const char *node_name, return -1; family = nft_str2family(family_str); - xfree(family_str); - if (family < 0) errno = EAFNOSUPPORT; diff --git a/src/rule.c b/src/rule.c index 4b5ffa6..c3cdb84 100644 --- a/src/rule.c +++ b/src/rule.c @@ -514,7 +514,7 @@ static int nft_rule_xml_parse(struct nft_rule *r, char *xml) if (r->table) xfree(r->table); - r->table = table; + r->table = strdup(table); r->flags |= (1 << NFT_RULE_ATTR_TABLE); chain = nft_mxml_str_parse(tree, "chain", MXML_DESCEND_FIRST); @@ -526,7 +526,7 @@ static int nft_rule_xml_parse(struct nft_rule *r, char *xml) if (r->chain) xfree(r->chain); - r->chain = chain; + r->chain = strdup(chain); r->flags |= (1 << NFT_RULE_ATTR_CHAIN); if (nft_mxml_num_parse(tree, "handle", MXML_DESCEND_FIRST, BASE_DEC, diff --git a/src/set.c b/src/set.c index 97856b3..ff34bf5 100644 --- a/src/set.c +++ b/src/set.c @@ -328,7 +328,7 @@ static int nft_set_xml_parse(struct nft_set *s, char *xml) if (s->name) xfree(s->name); - s->name = name; + s->name = strdup(name); s->flags |= (1 << NFT_SET_ATTR_NAME); table = nft_mxml_str_parse(tree, "table", MXML_DESCEND_FIRST); @@ -338,7 +338,7 @@ static int nft_set_xml_parse(struct nft_set *s, char *xml) if (s->table) xfree(s->table); - s->table = table; + s->table = strdup(table); s->flags |= (1 << NFT_SET_ATTR_TABLE); family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); diff --git a/src/table.c b/src/table.c index 1fa0dac..26bf60d 100644 --- a/src/table.c +++ b/src/table.c @@ -239,7 +239,7 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) if (t->name) xfree(t->name); - t->name = name; + t->name = strdup(name); t->flags |= (1 << NFT_TABLE_ATTR_NAME); family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); -- cgit v1.2.3