mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] tools:Fix a memory leak related to variable name
@ 2024-03-23  8:45 LuMingYin
  2024-03-23 18:22 ` Josh Poimboeuf
  0 siblings, 1 reply; 4+ messages in thread
From: LuMingYin @ 2024-03-23  8:45 UTC (permalink / raw)
  To: linux-kernel; +Cc: jpoimboe, peterz, LuMingYin

In the elf_create_prefix_symbol function defined in the /linux/tools/objtool/elf.c file, two pointer variables sym and name are defined. The program allocates dynamic memory for the pointer sym using the calloc function at line 822, and for the pointer name using the malloc function at line 824. When the if statement at line 826 returns true, the program returns at line 828. The content of the if statement at line 828 is if (sym==NULL || name==NULL), which checks if either sym or name is NULL. If this condition returns true, it indicates a situation where one of the pointers has successfully allocated memory but the other has not. Therefore, if the if statement returns true, directly returning may lead to memory leak issues. Hence, in the code, I have added checks separately for whether sym and name are NULL, and if they are not NULL, the corresponding dynamic memory spaces are freed.

Signed-off-by: LuMingYin <lumingyindetect@163.com>
---
 tools/objtool/elf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 3d27983dc908..42d20cd08936 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -825,6 +825,12 @@ elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
 
 	if (!sym || !name) {
 		perror("malloc");
+		if(sym){
+			free(sym);
+		}
+		if(name){
+			free(name);
+		}
 		return NULL;
 	}
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-24  5:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-23  8:45 [PATCH] tools:Fix a memory leak related to variable name LuMingYin
2024-03-23 18:22 ` Josh Poimboeuf
2024-03-24  5:56   ` lumingyindetect
2024-03-24  5:59   ` lumingyindetect

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®