In dimm_action(), actx.f_out and actx.f_in may be set by calling
fopen(). If exceptions occur, we will directly goto out tag.
However, we did not close actx.f_out|actx.f_in in out tag, which
will cause fd leakage.

Signed-off-by: Zhiqiang Liu <[email protected]>
---
 ndctl/dimm.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/ndctl/dimm.c b/ndctl/dimm.c
index 90eb0b8..2f52cda 100644
--- a/ndctl/dimm.c
+++ b/ndctl/dimm.c
@@ -1352,7 +1352,7 @@ static int dimm_action(int argc, const char **argv, 
struct ndctl_ctx *ctx,
                        fprintf(stderr, "failed to open: %s: (%s)\n",
                                        param.infile, strerror(errno));
                        rc = -errno;
-                       goto out;
+                       goto out_close_fout;
                }
        }

@@ -1371,7 +1371,7 @@ static int dimm_action(int argc, const char **argv, 
struct ndctl_ctx *ctx,
                fprintf(stderr, "'%s' is not a valid label version\n",
                                param.labelversion);
                rc = -EINVAL;
-               goto out;
+               goto out_close_fin_fout;
        }

        rc = 0;
@@ -1423,12 +1423,14 @@ static int dimm_action(int argc, const char **argv, 
struct ndctl_ctx *ctx,
                util_display_json_array(actx.f_out, actx.jdimms, flags);
        }

-       if (actx.f_out != stdout)
-               fclose(actx.f_out);
-
+ out_close_fin_fout:
        if (actx.f_in != stdin)
                fclose(actx.f_in);

+ out_close_fout:
+       if (actx.f_out != stdout)
+               fclose(actx.f_out);
+
  out:
        /*
         * count if some actions succeeded, 0 if none were attempted,
-- 
1.8.3.1


_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to