From 4a4ed5bf0c45c54d7d3b53812436af116d4b271d Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Sat, 2 Apr 2022 18:34:48 +0200 Subject: [PATCH] Fix memory leak on error path --- mx_proc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mx_proc.c b/mx_proc.c index 809a6d55..c5197f28 100644 --- a/mx_proc.c +++ b/mx_proc.c @@ -89,13 +89,11 @@ int mx_proc_pid_stat(struct mx_proc_pid_stat **pps, pid_t pid) pstat = *pps; if (!pstat) - pstat = mx_calloc_forever(1, sizeof(*pstat)); + *pps = pstat = mx_calloc_forever(1, sizeof(*pstat)); res = mx_proc_pid_stat_read(pstat, "/proc/%d/stat", pid); if (res < 0) return res; - - *pps = pstat; return 0; } @@ -370,9 +368,10 @@ int mx_proc_tree(struct mx_proc_tree **newtree) pps = NULL; res = mx_proc_pid_stat(&pps, pid); - if (res < 0) + if (res < 0) { + free(pps); continue; - + } mx_proc_tree_add(pt, pps); } free(namelist);