summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Engestrom <[email protected]>2019-07-07 11:40:04 +0100
committerJuan A. Suarez Romero <[email protected]>2019-08-08 10:28:03 +0000
commitd38952ef0d796599c2419e68b8e43cd4cfdb985c (patch)
tree3890d02a60947f7335ef5c015047cb6f324446cd /src
parent945a217e948cc0cad103d9306af31925355f7fe7 (diff)
util: fix mem leak of program path
Fixes: 759b94038987bb983398 ("util: Get program name based on path when possible") Signed-off-by: Eric Engestrom <[email protected]> Reviewed-by: Tapani Pälli <[email protected]> (cherry picked from commit 5b10ddf3589bdd6ef9cfc63a807ae91dc0e4095f)
Diffstat (limited to 'src')
-rw-r--r--src/util/u_process.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/util/u_process.c b/src/util/u_process.c
index cbccf48ed01..371335303ec 100644
--- a/src/util/u_process.c
+++ b/src/util/u_process.c
@@ -33,6 +33,15 @@
#undef GET_PROGRAM_NAME
#if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
+
+static char *path = NULL;
+
+static void __freeProgramPath()
+{
+ free(path);
+ path = NULL;
+}
+
static const char *
__getProgramName()
{
@@ -45,14 +54,10 @@ __getProgramName()
* Strip these arguments out by using the realpath only if it was
* a prefix of the invocation name.
*/
- static char *path;
-
- if (!path)
- /* Note: realpath() allocates memory that we will keep around for
- * the lifetime of the app, and then leak as the app closes.
- * FIXME: we should find a way to clean this properly
- */
+ if (!path) {
path = realpath("/proc/self/exe", NULL);
+ atexit(__freeProgramPath);
+ }
if (path && strncmp(path, program_invocation_name, strlen(path)) == 0) {
/* This shouldn't be null because path is a a prefix,