objtool: Fix memory leak in objtool_create_backup
PATCH 1/1 objtool: Fix memory leak in objtool_create_backup
From: Zong Cao <[email protected]>
The error handling branch did not properly free the memory before return, which would cause memory leak.
Fixes 8ad15c690084 ("objtool: Add --backup")
Signed-off-by Zong Cao <[email protected]>
--- tools/objtool/objtool.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
tools/objtool/objtool.c +16 -5
@@ -62,19 +62,19 @@ static bool objtool_create_backup(const char *_objname) d =open (name,O_CREAT |O_WRONLY |O_TRUNC ,0644 );if (d <0 ) {perror ("failed to create backup file" ); -return false; + goto free_name; } s =open (_objname,O_RDONLY );if (s <0 ) {perror ("failed to open orig file" ); -return false; + goto close_d; } buf =malloc (4096 );if (!buf) {perror ("failed backup data malloc" ); -return false; + goto close_s; }while ((l =read (s, buf,4096 )) >0 ) {
@@ -83,7 +83,7 @@ static bool objtool_create_backup(const char *_objname) t =write (d, base, l);if (t <0 ) {perror ("failed backup write" ); -return false; + goto free_buf; } base += t; l -= t;
@@ -92,7 +92,7 @@ static bool objtool_create_backup(const char *_objname)if (l <0 ) {perror ("failed backup read" ); -return false; + goto free_buf; }free (name);
@@ -101,6 +101,17 @@ static bool objtool_create_backup(const char *_objname)close (s);return true; + +free_buf: +free (buf); +close_s: +close (s); +close_d: +close (d); +free_name: +free (name); +return false; + }struct objtool_file *objtool_open_read (const char *_objname)
Signature · 1 line
2.30.1 (Apple Git-130)
Hi,
Quoted 4 lines · On Wed, 2 Mar 2022, [email protected] wrote:
From: Zong Cao <[email protected]>
The error handling branch did not properly free the memory before return, which would cause memory leak.
yes, but objtool exits immediately in those cases so this is not really needed in the end.
Regards
Miroslav