[PATCH 13/38] trace-cmd record: prevent a memory leak in show_error()

"Jerome Marchand" <[email protected]>
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
In show_error() the pointer p is used for several functions. At some
point it is used to contain the error log file. It's not freed before
being replaced by the result of read_file(path), which is not freed
either. Free p in both case.

Fixes a RESOURCE_LEAK error (CWE-772)

Signed-off-by: Jerome Marchand <[email protected]>
---
 tracecmd/trace-record.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index f05a58d1..3e29f922 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -2364,13 +2364,16 @@ static void show_error(const char *file, const char *type)
 			goto read_file;
 		}
 		read_error_log(p);
+		free(p);
 		goto out;
 	}
 
  read_file:
 	p = read_file(path);
-	if (p)
+	if (p) {
 		printf("%s", p);
+		free(p);
+	}
 
  out:
 	printf("Failed %s of %s\n", type, file);
-- 
2.44.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.