diff mbox series

[libubootenv,3/3] libuboot_exit: fix memory leaks

Message ID 20230331215011.50438-4-mhei@heimpold.de
State Accepted
Headers show
Series Fixes related to new YAML config file | expand

Commit Message

Michael Heimpold March 31, 2023, 9:50 p.m. UTC
Now that we might have set name and lockfile of the
structure, we need to free these fields, too.

It should also not be assumed that the list start is passed
as pointer.

Signed-off-by: Michael Heimpold <mhei@heimpold.de>
---
 src/uboot_env.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

Comments

Stefano Babic April 1, 2023, 8:50 a.m. UTC | #1
On 31.03.23 23:50, Michael Heimpold wrote:
> Now that we might have set name and lockfile of the
> structure, we need to free these fields, too.
> 
> It should also not be assumed that the list start is passed
> as pointer.
> 
> Signed-off-by: Michael Heimpold <mhei@heimpold.de>
> ---
>   src/uboot_env.c | 23 +++++++++++++++++++++--
>   1 file changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/src/uboot_env.c b/src/uboot_env.c
> index 64b0f55..1e079c9 100644
> --- a/src/uboot_env.c
> +++ b/src/uboot_env.c
> @@ -1909,8 +1909,27 @@ void libuboot_close(struct uboot_ctx *ctx) {
>   	}
>   }
>   
> -void libuboot_exit(struct uboot_ctx *ctx) {
> -	if (ctx->ctxlist)
> +void libuboot_exit(struct uboot_ctx *ctx)
> +{
> +	struct uboot_ctx *c;
> +	int i;
> +
> +	if (!ctx)
> +		return;
> +
> +	/* passed context might not be list start */
> +	if (ctx->ctxlist) {
>   		ctx = ctx->ctxlist;
> +	} else {
> +		/* but in case we don't have a list at all, fixup nelem so that
> +		 * we enter the loop to free the name and lockfile correctly */
> +		ctx->nelem = 1;
> +	}
> +
> +	for (i = 0, c = ctx; i < ctx->nelem; i++, c++) {
> +		free(c->name);
> +		free(c->lockfile);
> +	}
> +
>   	free(ctx);
>   }

Thanks, I forgot to do this.

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index 64b0f55..1e079c9 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -1909,8 +1909,27 @@  void libuboot_close(struct uboot_ctx *ctx) {
 	}
 }
 
-void libuboot_exit(struct uboot_ctx *ctx) {
-	if (ctx->ctxlist)
+void libuboot_exit(struct uboot_ctx *ctx)
+{
+	struct uboot_ctx *c;
+	int i;
+
+	if (!ctx)
+		return;
+
+	/* passed context might not be list start */
+	if (ctx->ctxlist) {
 		ctx = ctx->ctxlist;
+	} else {
+		/* but in case we don't have a list at all, fixup nelem so that
+		 * we enter the loop to free the name and lockfile correctly */
+		ctx->nelem = 1;
+	}
+
+	for (i = 0, c = ctx; i < ctx->nelem; i++, c++) {
+		free(c->name);
+		free(c->lockfile);
+	}
+
 	free(ctx);
 }