[v8] linuxapp, eal: Fix the memory leak issue of logid

Message ID 1536715910-38320-1-git-send-email-ziye.yang@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v8] linuxapp, eal: Fix the memory leak issue of logid |

Checks

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

Commit Message

Ziye Yang Sept. 12, 2018, 1:31 a.m. UTC
  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 memory leak issue.

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

Comments

Ferruh Yigit Oct. 16, 2018, 3:33 p.m. UTC | #1
On 9/12/2018 2:31 AM, Ziye Yang wrote:
> 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 memory leak issue.
> 
> Signed-off-by: Ziye Yang <ziye.yang@intel.com>

Reviewed-by: Ferruh Yigit <ferruh.yigit@intel.com>


check-git-log.sh complains about commit log, it can be:
eal/linux: fix memory leak of logid


Also needs:

Fixes: d8a2bc71dfc2 ("log: remove app path from syslog id")
CC: stable@dpdk.org
  
Thomas Monjalon Oct. 22, 2018, 8 a.m. UTC | #2
12/09/2018 03:31, Ziye Yang:
> 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 memory leak issue.
> 
> Signed-off-by: Ziye Yang <ziye.yang@intel.com>
> ---
> -	logid = strrchr(argv[0], '/');
> -	logid = strdup(logid ? logid + 1: argv[0]);
> -
> +	p = strrchr(argv[0], '/');
> +	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));

Shouldn't it be strlcpy instead of snprintf?
  
Thomas Monjalon Oct. 28, 2018, 10:41 a.m. UTC | #3
22/10/2018 10:00, Thomas Monjalon:
> 12/09/2018 03:31, Ziye Yang:
> > 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 memory leak issue.
> > 
> > Signed-off-by: Ziye Yang <ziye.yang@intel.com>
> > ---
> > -	logid = strrchr(argv[0], '/');
> > -	logid = strdup(logid ? logid + 1: argv[0]);
> > -
> > +	p = strrchr(argv[0], '/');
> > +	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
> 
> Shouldn't it be strlcpy instead of snprintf?

Applied with suggested replacement:

-       snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
+       strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
index e59ac65..46494f9 100644
--- a/lib/librte_eal/linuxapp/eal/eal.c
+++ b/lib/librte_eal/linuxapp/eal/eal.c
@@ -793,7 +793,8 @@  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;
+	const char *p;
+	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@  static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);