objtool: rework error handling in objtool_create_backup
PATCH 1/1 objtool: rework error handling in objtool_create_backup
The cppcheck reports this issue [tools/objtool/objtool.c:65]: (error) Memory leak: name
This is a general problem. When ojbtool_create_backup() fails anywhere it returns an error without freeing any of the buffers it allocates. So rework the error handler to goto the appropriate free level when an error occurs.
--- tools/objtool/objtool.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
tools/objtool/objtool.c +15 -8
@@ -25,6 +25,7 @@ static bool objtool_create_backup(const char *_objname) {int len =strlen (_objname);char *buf, *base, *name =malloc (len+6 ); +bool ret = false;int s, d, l, t;if (!name) {
@@ -38,19 +39,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 err1; } s =open (_objname,O_RDONLY );if (s <0 ) {perror ("failed to open orig file" ); -return false; + goto err2; } buf =malloc (4096 );if (!buf) {perror ("failed backup data malloc" ); -return false; + goto err3; }while ((l =read (s, buf,4096 )) >0 ) {
@@ -59,7 +60,7 @@ static bool objtool_create_backup(const char *_objname) t =write (d, base, l);if (t <0 ) {perror ("failed backup write" ); -return false; + goto err4; } base += t; l -= t;
@@ -68,15 +69,21 @@ static bool objtool_create_backup(const char *_objname)if (l <0 ) {perror ("failed backup read" ); -return false; + goto err4; } -free (name); + ret = true; + +err4:free (buf); -close (d); +err3:close (s); +err2: +close (d); +err1: +free (name); -return true; +return ret; }struct objtool_file *objtool_open_read (const char *_objname)
Signature · 1 line
2.27.0
Peter Zijlstra
maintainerQuoted 7 lines · Tom Rix
The cppcheck reports this issue [
tools/objtool/objtool.c:65]: (error) Memory leak: nameThis is a general problem. When
ojbtool_create_backup()fails anywhere it returns an error without freeing any of the buffers it allocates. So rework the error handler to goto the appropriate free level when an error occurs.
If it fails, objtool terminates. Process termination releases all resources.