chain: Fix bug. Check correct attribute
PATCH 1/3 chain: Fix bug. Check correct attribute
--- src/chain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/chain.c +1 -1
@@ -219,7 +219,7 @@ int nftnl_chain_set_data(struct nftnl_chain *c, uint16_t attr,xfree (c->dev); c->dev =strdup (data); -if (!c->type) +if (!c->dev)return -1 ;break ; }
Signature · 6 lines
2.8.3 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/3 fix some error checking in parser functions
Use a variable 'ret' multiple times without treat the error between can overwrite the previous error value, and may execute code which should not.
Bad way:
int f() {
int ret;
ret = g();
ret = h();
return ret;
}Good way:
int f() {
int ret;
ret = g();
if (ret)
return ret;
ret = h();
if (ret)
return ret;
return 0;
}--- src/rule.c | 14 ++++++++++---- src/set.c | 9 ++++++--- src/set_elem.c | 41 ++++++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 24 deletions(-)
src/rule.c +10 -4
@@ -427,7 +427,7 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) {struct nlattr *tb[NFTA_RULE_MAX +1 ] = {};struct nfgenmsg *nfg =mnl_nlmsg_get_payload (nlh); -int ret =0 ; +int ret;if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_rule_parse_attr_cb, tb) <0 )return -1 ;
@@ -452,10 +452,16 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) r->handle =be64toh (mnl_attr_get_u64 (tb[NFTA_RULE_HANDLE ])); r->flags |= (1 <<NFTNL_RULE_HANDLE ); } -if (tb[NFTA_RULE_EXPRESSIONS ]) +if (tb[NFTA_RULE_EXPRESSIONS ]) { ret =nftnl_rule_parse_expr (tb[NFTA_RULE_EXPRESSIONS ], r); -if (tb[NFTA_RULE_COMPAT ]) +if (ret) +return ret; + } +if (tb[NFTA_RULE_COMPAT ]) { ret =nftnl_rule_parse_compat (tb[NFTA_RULE_COMPAT ], r); +if (ret) +return ret; + }if (tb[NFTA_RULE_POSITION ]) { r->position =be64toh (mnl_attr_get_u64 (tb[NFTA_RULE_POSITION ])); r->flags |= (1 <<NFTNL_RULE_POSITION );
@@ -480,7 +486,7 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) r->family = nfg->nfgen_family; r->flags |= (1 <<NFTNL_RULE_FAMILY ); -return ret; +return 0 ; }EXPORT_SYMBOL_ALIAS (nftnl_rule_nlmsg_parse, nft_rule_nlmsg_parse);
src/set.c +6 -3
@@ -433,7 +433,7 @@ int nftnl_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s) {struct nlattr *tb[NFTA_SET_MAX +1 ] = {};struct nfgenmsg *nfg =mnl_nlmsg_get_payload (nlh); -int ret =0 ; +int ret;if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_set_parse_attr_cb, tb) <0 )return -1 ;
@@ -490,13 +490,16 @@ int nftnl_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s) s->gc_interval =ntohl (mnl_attr_get_u32 (tb[NFTA_SET_GC_INTERVAL ])); s->flags |= (1 <<NFTNL_SET_GC_INTERVAL ); } -if (tb[NFTA_SET_DESC ]) +if (tb[NFTA_SET_DESC ]) { ret =nftnl_set_desc_parse (s, tb[NFTA_SET_DESC ]); +if (ret) +return ret; + } s->family = nfg->nfgen_family; s->flags |= (1 <<NFTNL_SET_FAMILY ); -return ret; +return 0 ; }EXPORT_SYMBOL_ALIAS (nftnl_set_nlmsg_parse, nft_set_nlmsg_parse);
src/set_elem.c +24 -17
110 lines not on this page · raw message
Signature · 6 lines
2.8.3 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 2/3 libnftnl] fix some error checking in parser functions
Quoted 14 lines · Carlos Falgueras García
src/rule.c +1
-1@@ -427,7 +427,7 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) {struct nlattr *tb[NFTA_RULE_MAX +1 ] = {};struct nfgenmsg *nfg =mnl_nlmsg_get_payload (nlh); -int ret =0 ; +int ret;if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_rule_parse_attr_cb, tb) <0 )return -1 ;
src/rule.c int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) · line 458
Quoted 9 lines
src/rule.c +3
-2@@ -452,10 +452,16 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r) r->handle =be64toh (mnl_attr_get_u64 (tb[NFTA_RULE_HANDLE ])); r->flags |= (1 <<NFTNL_RULE_HANDLE ); } -if (tb[NFTA_RULE_EXPRESSIONS ]) +if (tb[NFTA_RULE_EXPRESSIONS ]) { ret =nftnl_rule_parse_expr (tb[NFTA_RULE_EXPRESSIONS ], r); -if (tb[NFTA_RULE_COMPAT ]) +if (ret) +return ret;
Please use:
if (ret < 0)
return ret;instead. I think this is the common idiom we're using all around this library.
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 3/3 Consolidate setters
In reply to Carlos Falgueras GarcíaUse setter method in all place where we set an attribute instead of repeat the code.
--- src/chain.c | 91 ++++++++++++++++++++++++-------------------------------- src/gen.c | 7 ++--- src/rule.c | 72 ++++++++++++++++----------------------------- src/set.c | 93 ++++++++++++++++++++++++---------------------------------- src/set_elem.c | 72 ++++++++++++++++++--------------------------- src/table.c | 30 ++++++++----------- 6 files changed, 148 insertions(+), 217 deletions(-)
src/chain.c +39 -52
@@ -465,14 +465,12 @@ static int nftnl_chain_parse_counters(struct nlattr *attr, struct nftnl_chain *cif (mnl_attr_parse_nested (attr, nftnl_chain_parse_counters_cb, tb) <0 )return -1 ; -if (tb[NFTA_COUNTER_PACKETS ]) { - c->packets =be64toh (mnl_attr_get_u64 (tb[NFTA_COUNTER_PACKETS ])); - c->flags |= (1 <<NFTNL_CHAIN_PACKETS ); - } -if (tb[NFTA_COUNTER_BYTES ]) { - c->bytes =be64toh (mnl_attr_get_u64 (tb[NFTA_COUNTER_BYTES ])); - c->flags |= (1 <<NFTNL_CHAIN_BYTES ); - } +if (tb[NFTA_COUNTER_PACKETS ]) +nftnl_chain_set_u64 (c,NFTNL_CHAIN_PACKETS , +be64toh (mnl_attr_get_u64 (tb[NFTA_COUNTER_PACKETS ]))); +if (tb[NFTA_COUNTER_BYTES ]) +nftnl_chain_set_u64 (c,NFTNL_CHAIN_BYTES , +be64toh (mnl_attr_get_u64 (tb[NFTA_COUNTER_BYTES ])));return 0 ; }
@@ -504,23 +502,22 @@ static int nftnl_chain_parse_hook_cb(const struct nlattr *attr, void *data)static int nftnl_chain_parse_hook (struct nlattr *attr,struct nftnl_chain *c) {struct nlattr *tb[NFTA_HOOK_MAX +1 ] = {}; +int err;if (mnl_attr_parse_nested (attr, nftnl_chain_parse_hook_cb, tb) <0 )return -1 ; -if (tb[NFTA_HOOK_HOOKNUM ]) { - c->hooknum =ntohl (mnl_attr_get_u32 (tb[NFTA_HOOK_HOOKNUM ])); - c->flags |= (1 <<NFTNL_CHAIN_HOOKNUM ); - } -if (tb[NFTA_HOOK_PRIORITY ]) { - c->prio =ntohl (mnl_attr_get_u32 (tb[NFTA_HOOK_PRIORITY ])); - c->flags |= (1 <<NFTNL_CHAIN_PRIO ); - } +if (tb[NFTA_HOOK_HOOKNUM ]) +nftnl_chain_set_u32 (c,NFTNL_CHAIN_HOOKNUM , +ntohl (mnl_attr_get_u32 (tb[NFTA_HOOK_HOOKNUM ]))); +if (tb[NFTA_HOOK_PRIORITY ]) +nftnl_chain_set_s32 (c,NFTNL_CHAIN_PRIO , +ntohl (mnl_attr_get_u32 (tb[NFTA_HOOK_PRIORITY ])));if (tb[NFTA_HOOK_DEV ]) { - c->dev =strdup (mnl_attr_get_str (tb[NFTA_HOOK_DEV ])); -if (!c->dev) -return -1 ; - c->flags |= (1 <<NFTNL_CHAIN_DEV ); + err =nftnl_chain_set_str (c,NFTNL_CHAIN_DEV , +mnl_attr_get_str (tb[NFTA_HOOK_DEV ])); +if (err) +return err; }return 0 ;
@@ -536,54 +533,44 @@ int nftnl_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_chain *c)return -1 ;if (tb[NFTA_CHAIN_NAME ]) { -if (c->flags & (1 <<NFTNL_CHAIN_NAME )) -xfree (c->name); - c->name =strdup (mnl_attr_get_str (tb[NFTA_CHAIN_NAME ])); -if (!c->name) -return -1 ; - c->flags |= (1 <<NFTNL_CHAIN_NAME ); + ret =nftnl_chain_set_str (c,NFTNL_CHAIN_NAME , +mnl_attr_get_str (tb[NFTA_CHAIN_NAME ])); +if (ret) +return ret; }if (tb[NFTA_CHAIN_TABLE ]) { -if (c->flags & (1 <<NFTNL_CHAIN_TABLE )) -xfree (c->table); - c->table =strdup (mnl_attr_get_str (tb[NFTA_CHAIN_TABLE ])); -if (!c->table) -return -1 ; - c->flags |= (1 <<NFTNL_CHAIN_TABLE ); + ret =nftnl_chain_set_str (c,NFTNL_CHAIN_TABLE , +mnl_attr_get_str (tb[NFTA_CHAIN_TABLE ])); +if (ret) +return ret; }if (tb[NFTA_CHAIN_HOOK ]) { ret =nftnl_chain_parse_hook (tb[NFTA_CHAIN_HOOK ], c);if (ret <0 )return ret; } -if (tb[NFTA_CHAIN_POLICY ]) { - c->policy =ntohl (mnl_attr_get_u32 (tb[NFTA_CHAIN_POLICY ])); - c->flags |= (1 <<NFTNL_CHAIN_POLICY ); - } -if (tb[NFTA_CHAIN_USE ]) { - c->use =ntohl (mnl_attr_get_u32 (tb[NFTA_CHAIN_USE ])); - c->flags |= (1 <<NFTNL_CHAIN_USE ); - } +if (tb[NFTA_CHAIN_POLICY ]) +nftnl_chain_set_u32 (c,NFTNL_CHAIN_POLICY , +ntohl (mnl_attr_get_u32 (tb[NFTA_CHAIN_POLICY ]))); +if (tb[NFTA_CHAIN_USE ]) +nftnl_chain_set_u32 (c,NFTNL_CHAIN_USE , +ntohl (mnl_attr_get_u32 (tb[NFTA_CHAIN_USE ])));if (tb[NFTA_CHAIN_COUNTERS ]) { ret =nftnl_chain_parse_counters (tb[NFTA_CHAIN_COUNTERS ], c);if (ret <0 )return ret; } -if (tb[NFTA_CHAIN_HANDLE ]) { - c->handle =be64toh (mnl_attr_get_u64 (tb[NFTA_CHAIN_HANDLE ])); - c->flags |= (1 <<NFTNL_CHAIN_HANDLE ); - } +if (tb[NFTA_CHAIN_HANDLE ]) +nftnl_chain_set_u64 (c,NFTNL_CHAIN_HANDLE , +be64toh (mnl_attr_get_u64 (tb[NFTA_CHAIN_HANDLE ])));if (tb[NFTA_CHAIN_TYPE ]) { -if (c->flags & (1 <<NFTNL_CHAIN_TYPE )) -xfree (c->type); - c->type =strdup (mnl_attr_get_str (tb[NFTA_CHAIN_TYPE ])); -if (!c->type) -return -1 ; - c->flags |= (1 <<NFTNL_CHAIN_TYPE ); + ret =nftnl_chain_set_str (c,NFTNL_CHAIN_TYPE , +mnl_attr_get_str (tb[NFTA_CHAIN_TYPE ])); +if (ret) +return ret; } - c->family = nfg->nfgen_family; - c->flags |= (1 <<NFTNL_CHAIN_FAMILY ); +nftnl_chain_set_u32 (c,NFTNL_CHAIN_FAMILY , nfg->nfgen_family);return ret; }
src/gen.c +3 -4
@@ -147,10 +147,9 @@ int nftnl_gen_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_gen *gen)if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_gen_parse_attr_cb, tb) <0 )return -1 ; -if (tb[NFTA_GEN_ID ]) { - gen->id =ntohl (mnl_attr_get_u32 (tb[NFTA_GEN_ID ])); - gen->flags |= (1 <<NFTNL_GEN_ID ); - } +if (tb[NFTA_GEN_ID ]) +nftnl_gen_set_u32 (gen,NFTNL_GEN_ID , +ntohl (mnl_attr_get_u32 (tb[NFTA_GEN_ID ])));return 0 ; }EXPORT_SYMBOL_ALIAS (nftnl_gen_nlmsg_parse, nft_gen_nlmsg_parse);
src/rule.c +26 -46
96 lines not on this page · raw message
src/set.c +39 -54
114 lines not on this page · raw message
src/set_elem.c +28 -44
106 lines not on this page · raw message
src/table.c +13 -17
@@ -222,29 +222,25 @@ int nftnl_table_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_table *t) {struct nlattr *tb[NFTA_TABLE_MAX +1 ] = {};struct nfgenmsg *nfg =mnl_nlmsg_get_payload (nlh); +int err;if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_table_parse_attr_cb, tb) <0 )return -1 ;if (tb[NFTA_TABLE_NAME ]) { -if (t->flags & (1 <<NFTNL_TABLE_NAME )) -xfree (t->name); - t->name =strdup (mnl_attr_get_str (tb[NFTA_TABLE_NAME ])); -if (!t->name) -return -1 ; - t->flags |= (1 <<NFTNL_TABLE_NAME ); + err =nftnl_table_set_str (t,NFTNL_TABLE_NAME , +mnl_attr_get_str (tb[NFTA_TABLE_NAME ])); +if (err) +return err; } -if (tb[NFTA_TABLE_FLAGS ]) { - t->table_flags =ntohl (mnl_attr_get_u32 (tb[NFTA_TABLE_FLAGS ])); - t->flags |= (1 <<NFTNL_TABLE_FLAGS ); - } -if (tb[NFTA_TABLE_USE ]) { - t->use =ntohl (mnl_attr_get_u32 (tb[NFTA_TABLE_USE ])); - t->flags |= (1 <<NFTNL_TABLE_USE ); - } - - t->family = nfg->nfgen_family; - t->flags |= (1 <<NFTNL_TABLE_FAMILY ); +if (tb[NFTA_TABLE_FLAGS ]) +nftnl_table_set_u32 (t,NFTNL_TABLE_FLAGS , +ntohl (mnl_attr_get_u32 (tb[NFTA_TABLE_FLAGS ]))); +if (tb[NFTA_TABLE_USE ]) +nftnl_table_set_u32 (t,NFTNL_TABLE_USE , +ntohl (mnl_attr_get_u32 (tb[NFTA_TABLE_USE ]))); + +nftnl_table_set_u32 (t,NFTNL_TABLE_FAMILY , nfg->nfgen_family);return 0 ; }
Signature · 6 lines
2.8.3 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Applied, thanks for catching up this.
Please, include descriptions in your future patches.
Moreover, I'd suggest you use a title with a single sentence.
I have rewritten your patch title and description.
http://git.netfilter.org/libnftnl/commit/?id=2fee091b0dd1741a8a87cafceaa0091adadd2b46