On 3/3/25 5:37 PM, alison.schofi...@intel.com wrote:
> From: Alison Schofield <alison.schofi...@intel.com>
>
> A coverity scan highlighted a resource leak caused by not freeing
> the open file descriptor upon exit of do_xaction_namespace().
>
> Move the fclose() to a 'goto out_close' and route all returns through
> that path.
>
> Signed-off-by: Alison Schofield <alison.schofi...@intel.com>
Reviewed-by: Dave Jiang <dave.ji...@intel.com>
> ---
> ndctl/namespace.c | 15 ++++++++-------
> 1 file changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/ndctl/namespace.c b/ndctl/namespace.c
> index bb0c2f2e28c7..5eb9e1e98e11 100644
> --- a/ndctl/namespace.c
> +++ b/ndctl/namespace.c
> @@ -2133,7 +2133,7 @@ static int do_xaction_namespace(const char *namespace,
> util_display_json_array(ri_ctx.f_out,
> ri_ctx.jblocks, 0);
> if (rc >= 0)
> (*processed)++;
> - return rc;
> + goto out_close;
> }
> }
>
> @@ -2144,11 +2144,11 @@ static int do_xaction_namespace(const char *namespace,
> rc = file_write_infoblock(param.outfile);
> if (rc >= 0)
> (*processed)++;
> - return rc;
> + goto out_close;
> }
>
> if (!namespace && action != ACTION_CREATE)
> - return rc;
> + goto out_close;
>
> if (namespace && (strcmp(namespace, "all") == 0))
> rc = 0;
> @@ -2207,7 +2207,7 @@ static int do_xaction_namespace(const char *namespace,
> saved_rc = rc;
> continue;
> }
> - return rc;
> + goto out_close;
> }
> ndctl_namespace_foreach_safe(region, ndns, _n) {
> ndns_name = ndctl_namespace_get_devname(ndns);
> @@ -2286,9 +2286,6 @@ static int do_xaction_namespace(const char *namespace,
> if (ri_ctx.jblocks)
> util_display_json_array(ri_ctx.f_out, ri_ctx.jblocks, 0);
>
> - if (ri_ctx.f_out && ri_ctx.f_out != stdout)
> - fclose(ri_ctx.f_out);
> -
> if (action == ACTION_CREATE && rc == -EAGAIN) {
> /*
> * Namespace creation searched through all candidate
> @@ -2303,6 +2300,10 @@ static int do_xaction_namespace(const char *namespace,
> else
> rc = -ENOSPC;
> }
> +
> +out_close:
> + if (ri_ctx.f_out && ri_ctx.f_out != stdout)
> + fclose(ri_ctx.f_out);
> if (saved_rc)
> rc = saved_rc;
>