From 86da7840bda24c71b0bfae8d1817bc35e99f3d64 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Thu, 9 Nov 2023 14:19:05 +0100 Subject: [PATCH] llext: fix a memory leak in an error case If a function fails it should release all the resources it has managed to acquire. Fix llext_load() to free memory that it has allocated in case of an error. Signed-off-by: Guennadi Liakhovetski --- subsys/llext/llext.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/subsys/llext/llext.c b/subsys/llext/llext.c index 60ecf60ec77..05878b5c72f 100644 --- a/subsys/llext/llext.c +++ b/subsys/llext/llext.c @@ -861,8 +861,11 @@ int llext_load(struct llext_loader *ldr, const char *name, struct llext **ext, ldr->hdr = ehdr; ret = do_llext_load(ldr, *ext, ldr_parm); - if (ret < 0) + if (ret < 0) { + k_heap_free(&llext_heap, *ext); + *ext = NULL; return ret; + } strncpy((*ext)->name, name, sizeof((*ext)->name)); (*ext)->name[sizeof((*ext)->name) - 1] = '\0';