Some covscan fixes
Phil Sutter
reviewercover Some covscan fixes
This series fixes a bunch of minor issues identified by Coverity tool.
Phil Sutter (10):
parser_bison: Fix for implicit declaration of isalnum
parser_json: Fix for memleak in tcp option error path
evaluate: Mark fall through case in str2hooknum()
json: Drop pointless assignment in exthdr_expr_json()
netlink: Avoid memleak in error path of netlink_delinearize_set()
netlink: Avoid memleak in error path of netlink_delinearize_chain()
netlink: Avoid memleak in error path of netlink_delinearize_table()
netlink: Avoid memleak in error path of netlink_delinearize_obj()
netlink_delinearize: Fix suspicious calloc() call
rule: Fix for potential off-by-one in cmd_add_loc()
src/evaluate.c | 1 + src/json.c | 1 - src/netlink.c | 7 +++++-- src/netlink_delinearize.c | 5 ++--- src/parser_bison.y | 1 + src/parser_json.c | 6 +++--- src/rule.c | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-)
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 1/10 parser_bison: Fix for implicit declaration of isalnum
Have to include ctype.h to make it known.
--- src/parser_bison.y | 1 + 1 file changed, 1 insertion(+)
src/parser_bison.y +1 -0
@@ -10,6 +10,7 @@ %{ +#include <ctype.h> #include <stddef.h> #include <stdio.h> #include <inttypes.h>
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 2/10 parser_json: Fix for memleak in tcp option error path
In reply to Phil SutterIf 'kind' value is invalid, the function returned without freeing 'expr' first. Fix this by performing the check before allocation.
--- src/parser_json.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
src/parser_json.c +3 -3
@@ -603,12 +603,12 @@ static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx, "base", &kind, "offset", &offset, "len", &len)) {uint32_t flag =0 ; - expr =tcpopt_expr_alloc (int_loc, kind, -TCPOPT_COMMON_KIND ); -if (kind <0 || kind >255 )return NULL ; + expr =tcpopt_expr_alloc (int_loc, kind, +TCPOPT_COMMON_KIND ); +if (offset ==TCPOPT_COMMON_KIND && len ==8 ) flag =NFT_EXTHDR_F_PRESENT ;
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 3/10 evaluate: Mark fall through case in str2hooknum()
In reply to Phil SutterIt is certainly intentional, so just mark it as such.
--- src/evaluate.c | 1 + 1 file changed, 1 insertion(+)
src/evaluate.c +1 -0
@@ -4145,6 +4145,7 @@ static uint32_t str2hooknum(uint32_t family, const char *hook)case NFPROTO_INET :if (!strcmp (hook,"ingress" ))return NF_INET_INGRESS ; +/* fall through */ case NFPROTO_IPV4 :case NFPROTO_BRIDGE :case NFPROTO_IPV6 :
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 4/10 json: Drop pointless assignment in exthdr_expr_json()
In reply to Phil SutterThe updated value of 'is_exists' is no longer read at this point.
--- src/json.c | 1 - 1 file changed, 1 deletion(-)
src/json.c +0 -1
@@ -708,7 +708,6 @@ json_t *exthdr_expr_json(const struct expr *expr, struct output_ctx *octx)"base" , expr->exthdr.raw_type,"offset" , expr->exthdr.offset, "len", expr->len); - is_exists = false; }return json_pack ("{s:o}" ,"tcp option" , root);
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 5/10 netlink: Avoid memleak in error path of netlink_delinearize_set()
In reply to Phil SutterDuplicate string 'comment' later when the function does not fail anymore.
--- src/netlink.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/netlink.c +2 -2
@@ -867,7 +867,7 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,if (ud[NFTNL_UDATA_SET_DATA_TYPEOF ]) typeof_expr_data =set_make_key (ud[NFTNL_UDATA_SET_DATA_TYPEOF ]);if (ud[NFTNL_UDATA_SET_COMMENT ]) - comment =xstrdup (nftnl_udata_get (ud[NFTNL_UDATA_SET_COMMENT ])); + comment =nftnl_udata_get (ud[NFTNL_UDATA_SET_COMMENT ]); } key =nftnl_set_get_u32 (nls,NFTNL_SET_KEY_TYPE );
@@ -905,7 +905,7 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx, set->handle.set.name =xstrdup (nftnl_set_get_str (nls,NFTNL_SET_NAME )); set->automerge = automerge;if (comment) - set->comment = comment; + set->comment =xstrdup (comment);init_list_head (&set_parse_ctx.stmt_list);
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 6/10 netlink: Avoid memleak in error path of netlink_delinearize_chain()
In reply to Phil SutterIf parsing udata fails, 'chain' has to be freed before returning to caller.
--- src/netlink.c | 1 + 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
@@ -599,6 +599,7 @@ struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx, udata =nftnl_chain_get_data (nlc,NFTNL_CHAIN_USERDATA , &ulen);if (nftnl_udata_parse (udata, ulen, chain_parse_udata_cb, ud) <0 ) {netlink_io_error (ctx,NULL ,"Cannot parse userdata" ); +chain_free (chain);return NULL ; }if (ud[NFTNL_UDATA_CHAIN_COMMENT ])
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 7/10 netlink: Avoid memleak in error path of netlink_delinearize_table()
In reply to Phil SutterIf parsing udata fails, 'table' has to be freed before returning to caller.
--- src/netlink.c | 1 + 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
@@ -647,6 +647,7 @@ struct table *netlink_delinearize_table(struct netlink_ctx *ctx, udata =nftnl_table_get_data (nlt,NFTNL_TABLE_USERDATA , &ulen);if (nftnl_udata_parse (udata, ulen, table_parse_udata_cb, ud) <0 ) {netlink_io_error (ctx,NULL ,"Cannot parse userdata" ); +table_free (table);return NULL ; }if (ud[NFTNL_UDATA_TABLE_COMMENT ])
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 8/10 netlink: Avoid memleak in error path of netlink_delinearize_obj()
In reply to Phil SutterIf parsing udata fails, 'obj' has to be freed before returning to caller.
--- src/netlink.c | 1 + 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
@@ -1450,6 +1450,7 @@ struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx, udata =nftnl_obj_get_data (nlo,NFTNL_OBJ_USERDATA , &ulen);if (nftnl_udata_parse (udata, ulen, obj_parse_udata_cb, ud) <0 ) {netlink_io_error (ctx,NULL ,"Cannot parse userdata" ); +obj_free (obj);return NULL ; }if (ud[NFTNL_UDATA_OBJ_COMMENT ])
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 9/10 netlink_delinearize: Fix suspicious calloc() call
In reply to Phil SutterParameter passed to sizeof() was wrong. While being at it, replace the whole call with xmalloc_array() which takes care of error checking.
--- src/netlink_delinearize.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
src/netlink_delinearize.c +2 -3
@@ -1733,9 +1733,8 @@ void expr_handler_init(void)unsigned int i;uint32_t hash; - expr_handle_ht =calloc (NFT_EXPR_HSIZE ,sizeof (expr_handle_ht)); -if (!expr_handle_ht) -memory_allocation_error (); + expr_handle_ht =xmalloc_array (NFT_EXPR_HSIZE , +sizeof (expr_handle_ht[0 ]));for (i =0 ; i <array_size (netlink_parsers); i++) { hash =djb_hash (netlink_parsers[i].name) %NFT_EXPR_HSIZE ;
Signature · 1 line
2.31.1
Phil Sutter
reviewerPATCH 10/10 rule: Fix for potential off-by-one in cmd_add_loc()
In reply to Phil SutterUsing num_attrs as index means it must be at max one less than the array's size at function start.
--- src/rule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
@@ -1275,7 +1275,7 @@ struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,void cmd_add_loc (struct cmd *cmd,uint16_t offset,const struct location *loc) { -if (cmd->num_attrs >NFT_NLATTR_LOC_MAX ) +if (cmd->num_attrs >=NFT_NLATTR_LOC_MAX )return ; cmd->attr[cmd->num_attrs].offset = offset;
Signature · 1 line
2.31.1