[PATCH] objtool: fix memory leak in tools/objtool/elf.c

liujing posted 1 patch 1 week, 2 days ago
tools/objtool/elf.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] objtool: fix memory leak in tools/objtool/elf.c
Posted by liujing 1 week, 2 days ago
If calloc succeeds but malloc fails (i.e., sym != NULL but name == NULL),
then memory has already been allocated for sym via calloc, but atthis
point sym is not freed, leading to a memory leak.

Signed-off-by: liujing <liujing@cmss.chinamobile.com>
---
 tools/objtool/elf.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index ca5d77db692a..69766f8a49d6 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -859,6 +859,8 @@ elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
 
 	if (!sym || !name) {
 		ERROR_GLIBC("malloc");
+		free(sym);
+		free(name);
 		return NULL;
 	}
 
-- 
2.27.0
Re: [PATCH] objtool: fix memory leak in tools/objtool/elf.c
Posted by Josh Poimboeuf 1 week, 2 days ago
On Mon, Sep 22, 2025 at 05:03:24PM +0800, liujing wrote:
> If calloc succeeds but malloc fails (i.e., sym != NULL but name == NULL),
> then memory has already been allocated for sym via calloc, but atthis
> point sym is not freed, leading to a memory leak.
> 
> Signed-off-by: liujing <liujing@cmss.chinamobile.com>

We intentionally don't fix most memory leaks, objtool has a short
runtime and all the memory gets freed on exit anyway.

-- 
Josh