linuxapp, eal: Fix the memory leak issue of logid

Message ID 1535605761-10196-1-git-send-email-ziye.yang@intel.com
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series
  • linuxapp, eal: Fix the memory leak issue of logid
Related show

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Ziye Yang Aug. 30, 2018, 5:09 a.m.
From: Ziye Yang <optimistyzy@gmail.com>

This patch is used to fix the memory leak issue of logid.
We use the ASAN test in SPDK when intergrating DPDK and
find this issue.

Signed-off-by: Ziye Yang <ziye.yang@intel.com>
---
 lib/librte_eal/linuxapp/eal/eal.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
index e59ac65..af8d73f 100644
--- a/lib/librte_eal/linuxapp/eal/eal.c
+++ b/lib/librte_eal/linuxapp/eal/eal.c
@@ -793,7 +793,7 @@  static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@  static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,7 @@  static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +838,7 @@  static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -844,6 +852,7 @@  static void rte_eal_init_alert(const char *msg)
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +860,7 @@  static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +871,7 @@  static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +880,7 @@  static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +905,7 @@  static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,6 +932,7 @@  static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -927,6 +941,7 @@  static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init VFIO\n");
 		rte_errno = EAGAIN;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 #endif
@@ -937,12 +952,14 @@  static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_memzone_init() < 0) {
 		rte_eal_init_alert("Cannot init memzone\n");
 		rte_errno = ENODEV;
+		free(logid);
 		return -1;
 	}
 
 	if (rte_eal_memory_init() < 0) {
 		rte_eal_init_alert("Cannot init memory\n");
 		rte_errno = ENOMEM;
+		free(logid);
 		return -1;
 	}
 
@@ -952,24 +969,28 @@  static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_malloc_heap_init() < 0) {
 		rte_eal_init_alert("Cannot init malloc heap\n");
 		rte_errno = ENODEV;
+		free(logid);
 		return -1;
 	}
 
 	if (rte_eal_tailqs_init() < 0) {
 		rte_eal_init_alert("Cannot init tail queues for objects\n");
 		rte_errno = EFAULT;
+		free(logid);
 		return -1;
 	}
 
 	if (rte_eal_alarm_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
 		/* rte_eal_alarm_init sets rte_errno on failure. */
+		free(logid);
 		return -1;
 	}
 
 	if (rte_eal_timer_init() < 0) {
 		rte_eal_init_alert("Cannot init HPET or TSC timers\n");
 		rte_errno = ENOTSUP;
+		free(logid);
 		return -1;
 	}
 
@@ -1024,6 +1045,7 @@  static void rte_eal_init_alert(const char *msg)
 	if (ret) {
 		rte_eal_init_alert("rte_service_init() failed\n");
 		rte_errno = ENOEXEC;
+		free(logid);
 		return -1;
 	}
 
@@ -1031,13 +1053,16 @@  static void rte_eal_init_alert(const char *msg)
 	if (rte_bus_probe()) {
 		rte_eal_init_alert("Cannot probe devices\n");
 		rte_errno = ENOTSUP;
+		free(logid);
 		return -1;
 	}
 
 #ifdef VFIO_PRESENT
 	/* Register mp action after probe() so that we got enough info */
-	if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
+	if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0) {
+		free(logid);
 		return -1;
+	}
 #endif
 
 	/* initialize default service/lcore mappings and start running. Ignore
@@ -1046,6 +1071,7 @@  static void rte_eal_init_alert(const char *msg)
 	ret = rte_service_start_with_defaults();
 	if (ret < 0 && ret != -ENOTSUP) {
 		rte_errno = ENOEXEC;
+		free(logid);
 		return -1;
 	}