[PATCH] tools:Fix a memory leak bug related to the cmd

LuMingYin posted 1 patch 1 year, 10 months ago
tools/objtool/check.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] tools:Fix a memory leak bug related to the cmd
Posted by LuMingYin 1 year, 10 months ago
In the function 'disas_funcs' defined in the file /linux/tools/objtool/check.c, a pointer variable named 'cmd' is declared (line 4583 of the file). This pointer variable allocates a block of dynamic memory using the 'malloc' function at line 4615. However, after using the memory area pointed to by 'cmd' (line 4619), it is not deallocated, resulting in a memory leak bug. This commit fixes the bug.

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

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index eb7e12ebc1d0..486a2a6374f5 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4617,6 +4617,7 @@ static int disas_funcs(const char *funcs)
 	/* real snprintf() */
 	snprintf(cmd, size, objdump_str, cross_compile, objname, funcs);
 	ret = system(cmd);
+	free(cmd);
 	if (ret) {
 		WARN("disassembly failed: %d", ret);
 		return -1;
-- 
2.25.1
Re: [PATCH] tools: Fix a memory leak in disas_funcs()
Posted by Markus Elfring 1 year, 9 months ago
…
> However, after using the memory area pointed to by 'cmd' (line 4619), it is not deallocated, resulting in a memory leak bug. This commit fixes the bug.

Please take advices from known information sources better into account.

* https://kernelnewbies.org/FirstKernelPatch

* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.9-rc5#n3


I got the impression that Josh Poimboeuf answered your development concern already.
https://lore.kernel.org/lkml/20240323182209.xqzb6rsswvfbg74n@treble/
https://lkml.org/lkml/2024/3/23/285


Would you like to support faster execution of the mentioned program
so that manual memory releases can be omitted so far?


Did your email address change in the meantime?

Regards,
Markus
Re:[PATCH] tools:Fix a memory leak bug related to the cmd
Posted by lumingyindetect@163.com 1 year, 10 months ago
Thank&nbsp;you&nbsp;for&nbsp;your&nbsp;prompt&nbsp;response!&nbsp;It&nbsp;is&nbsp;indeed&nbsp;a&nbsp;wise&nbsp;decision&nbsp;not&nbsp;to&nbsp;release&nbsp;dynamic&nbsp;memory&nbsp;when&nbsp;the&nbsp;tool&nbsp;runs&nbsp;briefly&nbsp;and&nbsp;encounters&nbsp;errors.&nbsp;However,&nbsp;I&nbsp;also&nbsp;noticed&nbsp;in&nbsp;the&nbsp;disas_funcs&nbsp;function&nbsp;in&nbsp;the&nbsp;/linux/tools/objtool/check.c&nbsp;file&nbsp;(line&nbsp;4617)&nbsp;that&nbsp;a&nbsp;variable&nbsp;named&nbsp;cmd&nbsp;pointing&nbsp;to&nbsp;a&nbsp;dynamic&nbsp;memory&nbsp;area&nbsp;is&nbsp;not&nbsp;being&nbsp;freed&nbsp;(regardless&nbsp;of&nbsp;whether&nbsp;an&nbsp;error&nbsp;occurs).&nbsp;In&nbsp;this&nbsp;case,&nbsp;would&nbsp;it&nbsp;be&nbsp;necessary&nbsp;to&nbsp;add&nbsp;a&nbsp;free(cmd)?
在 2024-03-23 14:55:50,LuMingYin <lumingyindetect@163.com> 写道:
In the function 'disas_funcs' defined in the file /linux/tools/objtool/check.c, a pointer variable named 'cmd' is declared (line 4583 of the file). This pointer variable allocates a block of dynamic memory using the 'malloc' function at line 4615. However, after using the memory area pointed to by 'cmd' (line 4619), it is not deallocated, resulting in a memory leak bug. This commit fixes the bug.

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

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index eb7e12ebc1d0..486a2a6374f5 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4617,6 +4617,7 @@ static int disas_funcs(const char *funcs)
 	/* real snprintf() */
 	snprintf(cmd, size, objdump_str, cross_compile, objname, funcs);
 	ret = system(cmd);
+	free(cmd);
 	if (ret) {
 		WARN("disassembly failed: %d", ret);
 		return -1;
-- 
2.25.1