Another round of Covscan indicated fixes
Phil Sutter
reviewercover Another round of Covscan indicated fixes
This series fixes potential issues identified by a recent Covscan run.
Phil Sutter (6):
nftnl_data_reg_snprintf: Add a missing break src/gen: Remove a pointless call to mnl_nlmsg_get_payload() src/object: Avoid returning garbage in nftnl_obj_do_parse() src/ruleset: Avoid reading garbage in nftnl_ruleset_cb() src/set_elem: Don't return garbage in nftnl_set_elems_parse() src/trace: Check return value of mnl_attr_parse_nested()
src/expr/data_reg.c | 1 + src/gen.c | 4 ++-- src/object.c | 2 +- src/ruleset.c | 10 +++++----- src/set_elem.c | 2 +- src/trace.c | 3 ++- 6 files changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH 1/6 nftnl_data_reg_snprintf: Add a missing break
The code works fine as-is, but if reg_type == DATA_VALUE && output_format == NFTNL_OUTPUT_XML, we fall through to DATA_CHAIN case and therefore pointlessly check output_format again.
--- src/expr/data_reg.c | 1 + 1 file changed, 1 insertion(+)
src/expr/data_reg.c +1 -0
@@ -207,6 +207,7 @@ int nftnl_data_reg_snprintf(char *buf, size_t size,default :break ; } +break ;case DATA_VERDICT :case DATA_CHAIN :switch (output_format) {
Signature · 1 line
2.13.1
Re: [libnftnl PATCH 1/6] nftnl_data_reg_snprintf: Add a missing break
Quoted 14 lines · Phil Sutter
The code works fine as-is, but if
reg_type==DATA_VALUE&&output_format==NFTNL_OUTPUT_XML, we fall through toDATA_CHAINcase and therefore pointlessly checkoutput_formatagain.Signed-off-by Phil Sutter <[email protected]>--- src/expr/data_reg.c | 1 + 1 file changed, 1 insertion(+)src/expr/data_reg.c +0
-0
src/expr/data_reg.c int nftnl_data_reg_snprintf(char *buf, size_t size, · line 213
Quoted 7 lines
src/expr/data_reg.c +1
-0@@ -207,6 +207,7 @@ int nftnl_data_reg_snprintf(char *buf, size_t size,default :break ; } +break ;case DATA_VERDICT :case DATA_CHAIN :switch (output_format) {
I'm going to add a break also at the end of 'case DATA_CHAIN:', recent gcc can spot warning on implicit fall through without comments, so let's fix that spot too.
Applied, thanks!
Phil Sutter
reviewerPATCH 2/6 src/gen: Remove a pointless call to mnl_nlmsg_get_payload()
In reply to Phil SutterIt is a common idiom in all *_nlmsg_parse() functions, but nftnl_gen_nlmsg_parse() doesn't make use of the data pointer and the compiler probably can't eliminate it since there could be a side-effect.
--- src/gen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/gen.c +2 -2
@@ -143,9 +143,9 @@ static int nftnl_gen_parse_attr_cb(const struct nlattr *attr, void *data)int nftnl_gen_nlmsg_parse (const struct nlmsghdr *nlh,struct nftnl_gen *gen) {struct nlattr *tb[NFTA_GEN_MAX +1 ] = {}; -struct nfgenmsg *nfg =mnl_nlmsg_get_payload (nlh); -if (mnl_attr_parse (nlh,sizeof (*nfg), nftnl_gen_parse_attr_cb, tb) <0 ) +if (mnl_attr_parse (nlh,sizeof (struct nfgenmsg ), + nftnl_gen_parse_attr_cb, tb) <0 )return -1 ;if (tb[NFTA_GEN_ID ]) {
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [libnftnl PATCH 2/6] src/gen: Remove a pointless call to mnl_nlmsg_get_payload()
Quoted 3 lines · Phil Sutter
It is a common idiom in all
*_nlmsg_parse()functions, butnftnl_gen_nlmsg_parse()doesn't make use of the data pointer and the compiler probably can't eliminate it since there could be a side-effect.
Also applied, thanks.
Phil Sutter
reviewerPATCH 3/6 src/object: Avoid returning garbage in nftnl_obj_do_parse()
In reply to Phil SutterIt may happen that 'perr' variable does not get initialized, so making parameter 'err' point to it in any case is error-prone. Avoid this by initializing 'perr' upon declaration.
--- src/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/object.c +1 -1
@@ -358,7 +358,7 @@ static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type, const void *data, struct nftnl_parse_err *err, enum nftnl_parse_input input) { - struct nftnl_parse_err perr; + struct nftnl_parse_err perr = {};int ret;switch (type) {
Signature · 1 line
2.13.1
Re: [libnftnl PATCH 3/6] src/object: Avoid returning garbage in nftnl_obj_do_parse()
Quoted 3 lines · Phil Sutter
It may happen that 'perr' variable does not get initialized, so making parameter 'err' point to it in any case is error-prone. Avoid this by initializing 'perr' upon declaration.
Applied, thanks.
Phil Sutter
reviewerPATCH 4/6 src/ruleset: Avoid reading garbage in nftnl_ruleset_cb()
In reply to Phil SutterIf nftnl_ruleset_json_parse() is called with arg == NULL, ctx.data is left uninitialized and will later be used in nftnl_ruleset_cb(). Avoid this by using a C99-style initializer for 'ctx' which sets all omitted fields to zero.
--- src/ruleset.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
src/ruleset.c +5 -5
@@ -519,11 +519,11 @@ static int nftnl_ruleset_json_parse(const void *json,json_error_t error;int i, len;const char *key; -struct nftnl_parse_ctx ctx; - - ctx.cb = cb; - ctx.format = type; - ctx.flags =0 ; +struct nftnl_parse_ctx ctx = { + .cb = cb, + .format = type, + .flags =0 , + }; ctx.set_list =nftnl_set_list_alloc ();if (ctx.set_list ==NULL )
Signature · 1 line
2.13.1
Re: [libnftnl PATCH 4/6] src/ruleset: Avoid reading garbage in nftnl_ruleset_cb()
Quoted 4 lines · Phil Sutter
If
nftnl_ruleset_json_parse()is called with arg == NULL, ctx.data is left uninitialized and will later be used innftnl_ruleset_cb(). Avoid this by using a C99-style initializer for 'ctx' which sets all omitted fields to zero.
Applied, thanks Phil.
Phil Sutter
reviewerPATCH 5/6 src/set_elem: Don't return garbage in nftnl_set_elems_parse()
In reply to Phil SutterThis might happen if netlink message is malformed (no nested attributes are present), so treat this as an error and return -1 instead of garbage to caller.
--- src/set_elem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/set_elem.c +1 -1
@@ -490,7 +490,7 @@ nftnl_set_elem_list_parse_attr_cb(const struct nlattr *attr, void *data)static int nftnl_set_elems_parse (struct nftnl_set *s,const struct nlattr *nest) {struct nlattr *attr; -int ret; +int ret =-1 ;mnl_attr_for_each_nested (attr, nest) {if (mnl_attr_get_type (attr) !=NFTA_LIST_ELEM )
Signature · 1 line
2.13.1
Re: [libnftnl PATCH 5/6] src/set_elem: Don't return garbage in nftnl_set_elems_parse()
Quoted 3 lines · Phil Sutter
This might happen if netlink message is malformed (no nested attributes are present), so treat this as an error and return -1 instead of garbage to caller.
Applied, thanks.
Phil Sutter
reviewerPATCH 6/6 src/trace: Check return value of mnl_attr_parse_nested()
In reply to Phil SutterThis is done everywhere else as well, so certainly not a bad thing here either.
--- src/trace.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/trace.c +2 -1
@@ -301,7 +301,8 @@ static int nftnl_trace_parse_verdict(const struct nlattr *attr, {struct nlattr *tb[NFTA_VERDICT_MAX +1 ]; -mnl_attr_parse_nested (attr, nftnl_trace_parse_verdict_cb, tb); +if (mnl_attr_parse_nested (attr, nftnl_trace_parse_verdict_cb, tb) <0 ) +return -1 ;if (!tb[NFTA_VERDICT_CODE ])abi_breakage ();
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [libnftnl PATCH 6/6] src/trace: Check return value of mnl_attr_parse_nested()
Quoted 2 lines · Phil Sutter
This is done everywhere else as well, so certainly not a bad thing here either.
Indeed. Applied, thanks Phil.