From d4809085e24a66239d11f7269cfec24920c1642a Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Tue, 6 Aug 2013 11:16:02 +0200 Subject: src: xml: consolidate error path in table and chain objects Remove a good bunch of LOC with this cleanup. Signed-off-by: Pablo Neira Ayuso --- src/chain.c | 97 ++++++++++++++++++++----------------------------------------- src/table.c | 32 ++++++++------------ 2 files changed, 44 insertions(+), 85 deletions(-) (limited to 'src') diff --git a/src/chain.c b/src/chain.c index 3ad52fd..e2e6f90 100644 --- a/src/chain.c +++ b/src/chain.c @@ -585,65 +585,49 @@ err: static int nft_chain_xml_parse(struct nft_chain *c, char *xml) { #ifdef XML_PARSING - mxml_node_t *tree = NULL; - mxml_node_t *node = NULL; + mxml_node_t *tree; + mxml_node_t *node; const char *name; const char *hooknum_str; int family, hooknum; - /* NOTE: all XML nodes are mandatory */ - - /* Load the tree */ tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); if (tree == NULL) return -1; - if (strcmp(tree->value.opaque, "chain") != 0) { - mxmlDelete(tree); - return -1; - } + if (strcmp(tree->value.opaque, "chain") != 0) + goto err; name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST); - if (name == NULL) { - mxmlDelete(tree); - return -1; - } + if (name == NULL) + 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, - &c->handle, NFT_TYPE_U64) != 0) { - mxmlDelete(tree); - return -1; - } + &c->handle, NFT_TYPE_U64) != 0) + goto err; c->flags |= (1 << NFT_CHAIN_ATTR_HANDLE); if (nft_mxml_num_parse(tree, "bytes", MXML_DESCEND_FIRST, BASE_DEC, - &c->bytes, NFT_TYPE_U64) != 0) { - mxmlDelete(tree); - return -1; - } + &c->bytes, NFT_TYPE_U64) != 0) + goto err; c->flags |= (1 << NFT_CHAIN_ATTR_BYTES); if (nft_mxml_num_parse(tree, "packets", MXML_DESCEND_FIRST, BASE_DEC, - &c->packets, NFT_TYPE_U64) != 0) { - mxmlDelete(tree); - return -1; - } + &c->packets, NFT_TYPE_U64) != 0) + goto err; c->flags |= (1 << NFT_CHAIN_ATTR_PACKETS); - /* Get and set */ node = mxmlFindElement(tree, tree, "type", NULL, NULL, MXML_DESCEND_FIRST); - if (node == NULL) { - mxmlDelete(tree); - return -1; - } + if (node == NULL) + goto err; if (c->type) xfree(c->type); @@ -651,78 +635,61 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) c->type = strdup(node->child->value.opaque); c->flags |= (1 << NFT_CHAIN_ATTR_TYPE); - /* Get and set */ node = mxmlFindElement(tree, tree, "table", NULL, NULL, MXML_DESCEND); - if (node == NULL) { - mxmlDelete(tree); - return -1; - } + if (node == NULL) + goto err; + if (c->table) xfree(c->table); c->table = strdup(node->child->value.opaque); c->flags |= (1 << NFT_CHAIN_ATTR_TABLE); - /* Get and set */ if (nft_mxml_num_parse(tree, "prio", MXML_DESCEND, BASE_DEC, &c->prio, - NFT_TYPE_S32) != 0) { - mxmlDelete(tree); - return -1; - } + NFT_TYPE_S32) != 0) + goto err; c->flags |= (1 << NFT_CHAIN_ATTR_PRIO); - /* Ignore (cannot be set)*/ - node = mxmlFindElement(tree, tree, "use", NULL, NULL, MXML_DESCEND); - - hooknum_str = nft_mxml_str_parse(tree, "hooknum", MXML_DESCEND_FIRST); - if (hooknum_str == NULL) { - mxmlDelete(tree); - return -1; - } + if (hooknum_str == NULL) + goto err; hooknum = nft_str2hooknum(hooknum_str); xfree(hooknum_str); - if (hooknum < 0) { - mxmlDelete(tree); - return -1; - } + if (hooknum < 0) + goto err; c->hooknum = hooknum; c->flags |= (1 << NFT_CHAIN_ATTR_HOOKNUM); - /* Get and set */ node = mxmlFindElement(tree, tree, "policy", NULL, NULL, MXML_DESCEND); - if (node == NULL) { - mxmlDelete(tree); - return -1; - } + if (node == NULL) + goto err; if (strcmp(node->child->value.opaque, "accept") == 0) { c->policy = NF_ACCEPT; } else if (strcmp(node->child->value.opaque, "drop") == 0) { c->policy = NF_DROP; - } else { - mxmlDelete(tree); - return -1; - } + } else + goto err; c->flags |= (1 << NFT_CHAIN_ATTR_POLICY); /* Get and set */ family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); - if (family < 0) { - mxmlDelete(tree); - return -1; - } + if (family < 0) + goto err; c->family = family; c->flags |= (1 << NFT_CHAIN_ATTR_FAMILY); mxmlDelete(tree); return 0; +err: + mxmlDelete(tree); + return -1; #else errno = EOPNOTSUPP; return -1; diff --git a/src/table.c b/src/table.c index bb66717..baad801 100644 --- a/src/table.c +++ b/src/table.c @@ -221,27 +221,20 @@ EXPORT_SYMBOL(nft_table_nlmsg_parse); static int nft_table_xml_parse(struct nft_table *t, char *xml) { #ifdef XML_PARSING - mxml_node_t *tree = NULL; + mxml_node_t *tree; const char *name; int family; - /* NOTE: all XML nodes are mandatory */ - - /* Load the tree */ tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); if (tree == NULL) return -1; - if (strcmp(tree->value.opaque, "table") != 0) { - mxmlDelete(tree); - return -1; - } + if (strcmp(tree->value.opaque, "table") != 0) + goto err; name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST); - if (name == NULL) { - mxmlDelete(tree); - return -1; - } + if (name == NULL) + goto err; if (t->name) xfree(t->name); @@ -250,24 +243,23 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) t->flags |= (1 << NFT_TABLE_ATTR_NAME); family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); - if (family < 0) { - mxmlDelete(tree); - return -1; - } + if (family < 0) + goto err; t->family = family; t->flags |= (1 << NFT_TABLE_ATTR_FAMILY); if (nft_mxml_num_parse(tree, "table_flags", MXML_DESCEND, BASE_DEC, - &t->table_flags, NFT_TYPE_U32) != 0) { - mxmlDelete(tree); - return -1; - } + &t->table_flags, NFT_TYPE_U32) != 0) + goto err; t->flags |= (1 << NFT_TABLE_ATTR_FLAGS); mxmlDelete(tree); return 0; +err: + mxmlDelete(tree); + return -1; #else errno = EOPNOTSUPP; return -1; -- cgit v1.2.3