Fix some covscan findings
Phil Sutter
reviewercover Fix some covscan findings
All these are rather minor issues, no big deal.
Phil Sutter (5):
optimize: Clarify chain_optimize() array allocations
optimize: Do not return garbage from stack
netlink: Fix for potential NULL-pointer deref
meta: parse_iso_date() returns boolean
mnl: dump_nf_hooks() leaks memory in error path
src/meta.c | 2 +- src/mnl.c | 11 +++++++++-- src/netlink.c | 3 ++- src/optimize.c | 9 +++++---- 4 files changed, 17 insertions(+), 8 deletions(-)
Signature · 1 line
2.38.0
Phil Sutter
reviewerPATCH 1/5 optimize: Clarify chain_optimize() array allocations
Arguments passed to sizeof() where deemed suspicious by covscan due to the different type. Consistently specify size of an array 'a' using 'sizeof(*a) * nmemb'.
For the statement arrays in stmt_matrix, even use xzalloc_array() since the item count is fixed and therefore can't be zero.
--- src/optimize.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
src/optimize.c +4 -3
@@ -1113,10 +1113,11 @@ static int chain_optimize(struct nft_ctx *nft, struct list_head *rules) ctx->num_rules++; } - ctx->rule =xzalloc (sizeof (ctx->rule) * ctx->num_rules); - ctx->stmt_matrix =xzalloc (sizeof (struct stmt *) * ctx->num_rules); + ctx->rule =xzalloc (sizeof (*ctx->rule) * ctx->num_rules); + ctx->stmt_matrix =xzalloc (sizeof (*ctx->stmt_matrix) * ctx->num_rules);for (i =0 ; i < ctx->num_rules; i++) - ctx->stmt_matrix[i] =xzalloc (sizeof (struct stmt *) *MAX_STMTS ); + ctx->stmt_matrix[i] =xzalloc_array (MAX_STMTS , +sizeof (**ctx->stmt_matrix)); merge =xzalloc (sizeof (*merge) * ctx->num_rules);
Signature · 1 line
2.38.0
Phil Sutter
reviewerPATCH 2/5 optimize: Do not return garbage from stack
In reply to Phil SutterFrom: Phil Sutter <[email protected]>
If input does not contain a single 'add' command (unusual, but possible), 'ret' value was not initialized by nft_optimize() before returning its value.
--- src/optimize.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/optimize.c +1 -1
@@ -1217,7 +1217,7 @@ static int cmd_optimize(struct nft_ctx *nft, struct cmd *cmd)int nft_optimize (struct nft_ctx *nft,struct list_head *cmds) {struct cmd *cmd; -int ret; +int ret =0 ;list_for_each_entry (cmd, cmds, list) {switch (cmd->op) {
Signature · 1 line
2.38.0
Phil Sutter
reviewerPATCH 3/5 netlink: Fix for potential NULL-pointer deref
In reply to Phil SutterIf memory allocation fails, calloc() returns NULL which was not checked for. The code seems to expect zero array size though, so simply replacing this call by one of the x*calloc() ones won't work. So guard the call also by a check for 'len'.
--- src/netlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/netlink.c +2 -1
@@ -1790,7 +1790,8 @@ netlink_delinearize_flowtable(struct netlink_ctx *ctx,while (dev_array[len]) len++; - flowtable->dev_array =calloc (1 , len *sizeof (char *)); +if (len) + flowtable->dev_array =xmalloc (len *sizeof (char *));for (i =0 ; i < len; i++) flowtable->dev_array[i] =xstrdup (dev_array[i]);
Signature · 1 line
2.38.0
Phil Sutter
reviewerPATCH 4/5 meta: parse_iso_date() returns boolean
In reply to Phil SutterReturning ts if 'ts == (time_t) -1' signals success to caller despite failure.
--- src/meta.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/meta.c +1 -1
@@ -433,7 +433,7 @@ static bool parse_iso_date(uint64_t *tstamp, const char *sym) cur_tm =localtime (&ts);if (ts == (time_t) -1 || cur_tm ==NULL ) -return ts; +return false;/* Substract tm_gmtoff to get the current time */ *tstamp = ts - cur_tm->tm_gmtoff;
Signature · 1 line
2.38.0
Phil Sutter
reviewerPATCH 5/5 mnl: dump_nf_hooks() leaks memory in error path
In reply to Phil SutterHave to free the basehook object before returning to caller.
--- src/mnl.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
src/mnl.c +9 -2
@@ -2217,16 +2217,23 @@ static int dump_nf_hooks(const struct nlmsghdr *nlh, void *_data)struct nlattr *nested[NFNLA_HOOK_INFO_MAX +1 ] = {};uint32_t type; -if (mnl_attr_parse_nested (tb[NFNLA_HOOK_CHAIN_INFO ], dump_nf_chain_info_cb, nested) <0 ) +if (mnl_attr_parse_nested (tb[NFNLA_HOOK_CHAIN_INFO ], + dump_nf_chain_info_cb, nested) <0 ) { +basehook_free (hook);return -1 ; + } type =ntohl (mnl_attr_get_u32 (nested[NFNLA_HOOK_INFO_TYPE ]));if (type ==NFNL_HOOK_TYPE_NFTABLES ) {struct nlattr *info[NFNLA_CHAIN_MAX +1 ] = {};const char *tablename, *chainname; -if (mnl_attr_parse_nested (nested[NFNLA_HOOK_INFO_DESC ], dump_nf_attr_chain_cb, info) <0 ) +if (mnl_attr_parse_nested (nested[NFNLA_HOOK_INFO_DESC ], + dump_nf_attr_chain_cb, + info) <0 ) { +basehook_free (hook);return -1 ; + } tablename =mnl_attr_get_str (info[NFNLA_CHAIN_TABLE ]); chainname =mnl_attr_get_str (info[NFNLA_CHAIN_NAME ]);
Signature · 1 line
2.38.0
Phil Sutter
reviewerQuoted 1 line · Phil Sutter
All these are rather minor issues, no big deal.
Series applied after fixing up my email address in one of them.