diff mbox series

framework: fix memory leaks caused by fwts_log_get_filenames

Message ID 20240129071739.61149-1-ivan.hu@canonical.com
State Accepted
Headers show
Series framework: fix memory leaks caused by fwts_log_get_filenames | expand

Commit Message

ivanhu Jan. 29, 2024, 7:17 a.m. UTC
BUgLink: https://bugs.launchpad.net/fwts/+bug/2051480

Got below resource leak error from the coverity scan

*** CID 343913: Resource leaks (RESOURCE_LEAK)
/src/lib/src/fwts_framework.c: 1567 in fwts_framework_args()
1561 /* Results log */
1562 if ((fw->results = fwts_log_open("fwts",
1563 fw->results_logname,
1564 (fw->flags & FWTS_FLAG_FORCE_CLEAN) ? "w" : "a",
1565 fw->log_type)) == NULL) {
1566 ret = FWTS_ERROR;
>>> CID 343913: Resource leaks (RESOURCE_LEAK)
>>> Failing to save or free storage allocated by
"fwts_log_get_filenames(fw->results_logname, fw->log_type)" leaks it.
1567 fprintf(stderr, "%s: Cannot open results log '%s'"
1568 " (you may need to remove it to set proper"
1569 " permissions).\n",
1570 argv[0],
1571 fwts_log_get_filenames(fw->results_logname, fw->log_type));
1572 goto tidy_close;

Signed-off-by: Ivan Hu <ivan.hu@canonical.com>
---
 src/lib/src/fwts_framework.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
index 41124394..b4d3c885 100644
--- a/src/lib/src/fwts_framework.c
+++ b/src/lib/src/fwts_framework.c
@@ -1563,12 +1563,16 @@  int fwts_framework_args(const int argc, char **argv)
 			fw->results_logname,
 			(fw->flags & FWTS_FLAG_FORCE_CLEAN) ? "w" : "a",
 			fw->log_type)) == NULL) {
+		char *filenames = fwts_log_get_filenames(fw->results_logname, fw->log_type);
+
 		ret = FWTS_ERROR;
 		fprintf(stderr, "%s: Cannot open results log '%s'"
 			" (you may need to remove it to set proper"
 			" permissions).\n",
 			argv[0],
-			fwts_log_get_filenames(fw->results_logname, fw->log_type));
+			filenames ? filenames : fw->results_logname);
+		if (filenames)
+			free(filenames);
 		goto tidy_close;
 	}