[PATCH] tools:Fix a memory leak related to new_alt_group

From: LuMingYin
Date: Sat Mar 23 2024 - 03:07:38 EST


In the function 'handle_group_alt' defined in the file /linux/tools/objtool/check.c, a pointer variable named 'new_alt_group' is declared (line 1748 of the file). At line 1791 of the file, the 'malloc' function is used to allocate a block of dynamic memory for this pointer. When the if statement at line 1792 returns false, it indicates successful allocation of dynamic memory. However, when the if statement at line 1806 returns false, the program returns at line 1808. During this process, the dynamic memory area pointed to by 'new_alt_group' is neither used nor deallocated, leading to a memory leak bug. This commit fixes this issue.

Signed-off-by: LuMingYin <lumingyindetect@xxxxxxx>
---
tools/objtool/check.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index eb7e12ebc1d0..f5d8aa79e0cc 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1805,6 +1805,7 @@ static int handle_group_alt(struct objtool_file *file,
nop = malloc(sizeof(*nop));
if (!nop) {
WARN("malloc failed");
+ free(new_alt_group);
return -1;
}
memset(nop, 0, sizeof(*nop));
--
2.25.1