[ndctl,1/4] ndctl, keys: fix a memory leak in dimm_create_key()
diff mbox series

Message ID 20190202015253.20051-1-vishal.l.verma@intel.com
State New, archived
Headers show
Series
  • [ndctl,1/4] ndctl, keys: fix a memory leak in dimm_create_key()
Related show

Commit Message

Vishal Verma Feb. 2, 2019, 1:52 a.m. UTC
Static analysis reports that we are leaking 'fp' in the above function.
Add an fclose() to close the leak.

Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 ndctl/util/keys.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Dan Williams Feb. 2, 2019, 1:58 a.m. UTC | #1
On Fri, Feb 1, 2019 at 5:53 PM Vishal Verma <vishal.l.verma@intel.com> wrote:
>
> Static analysis reports that we are leaking 'fp' in the above function.
> Add an fclose() to close the leak.
>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
>  ndctl/util/keys.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
>
> diff --git a/ndctl/util/keys.c b/ndctl/util/keys.c
> index 85818c0..acc2ff3 100644
> --- a/ndctl/util/keys.c
> +++ b/ndctl/util/keys.c
> @@ -260,14 +260,15 @@ static key_serial_t dimm_create_key(struct ndctl_dimm *dimm,
>
>          wrote = fwrite(buffer, 1, size, fp);
>          if (wrote != size) {
> -                if (wrote == -1)
> -                        rc = -errno;
> -                else
> -                        rc = -EIO;
> -                fprintf(stderr, "Failed to write to %s: %s\n",
> -                                path, strerror(-rc));
> -                free(buffer);
> -                return rc;
> +               if (wrote == -1)
> +                       rc = -errno;
> +               else
> +                       rc = -EIO;
> +               fprintf(stderr, "Failed to write to %s: %s\n",
> +                               path, strerror(-rc));
> +               fclose(fp);
> +               free(buffer);
> +               return rc;
>          }
>
>          fclose(fp);

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

Patch
diff mbox series

diff --git a/ndctl/util/keys.c b/ndctl/util/keys.c
index 85818c0..acc2ff3 100644
--- a/ndctl/util/keys.c
+++ b/ndctl/util/keys.c
@@ -260,14 +260,15 @@  static key_serial_t dimm_create_key(struct ndctl_dimm *dimm,
 
 	 wrote = fwrite(buffer, 1, size, fp);
 	 if (wrote != size) {
-		 if (wrote == -1)
-			 rc = -errno;
-		 else
-			 rc = -EIO;
-		 fprintf(stderr, "Failed to write to %s: %s\n",
-				 path, strerror(-rc));
-		 free(buffer);
-		 return rc;
+		if (wrote == -1)
+			rc = -errno;
+		else
+			rc = -EIO;
+		fprintf(stderr, "Failed to write to %s: %s\n",
+				path, strerror(-rc));
+		fclose(fp);
+		free(buffer);
+		return rc;
 	 }
 
 	 fclose(fp);