[XEN PATCH] tools/console/daemon: fix log_dir memory leak in xenconsoled

Andrew Mbugua posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20260813212724.2607832-1-andrewprecious388@gmail.com
tools/console/daemon/main.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[XEN PATCH] tools/console/daemon: fix log_dir memory leak in xenconsoled
Posted by Andrew Mbugua 2 weeks ago
Valgrind reports that 21 bytes are "still reachable" from the (XEN_LOG_DIR "/console") allocation:

HEAP SUMMARY:
    in use at exit: 21 bytes in 1 blocks
    total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated

Since the dynamic memory allocation for the default log directory path happens before the process
forks into the background, the parent and intermediate processes exit during daemonize()
with the memory still reachable.

Move the strdup() allocation down below the daemonize() block. This ensures only the final
background daemon allocates the default path, matching the lifetime of the free(log_dir)
cleanup loop at the exit of main().

With this change, Valgrind reports a clean heap summary

HEAP SUMMARY:
    in use at exit: 0 bytes in 0 blocks
    total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated

Signed-off-by: Andrew Mbugua <andrewprecious388@gmail.com>
---
 tools/console/daemon/main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/console/daemon/main.c b/tools/console/daemon/main.c
index aac7233a48..9f81d73164 100644
--- a/tools/console/daemon/main.c
+++ b/tools/console/daemon/main.c
@@ -181,10 +181,6 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (!log_dir) {
-		log_dir = strdup(XEN_LOG_DIR "/console");
-	}
-
 	if (geteuid() != 0) {
 		fprintf(stderr, "%s requires root to run.\n", argv[0]);
 		exit(EPERM);
@@ -201,6 +197,10 @@ int main(int argc, char **argv)
 		daemonize(pidfile ? pidfile : XEN_RUN_DIR "/xenconsoled.pid");
 	}
 
+	if (!log_dir) {
+                log_dir = strdup(XEN_LOG_DIR "/console");
+        }
+
 	if (!xen_setup())
 		exit(1);
 
-- 
2.47.3
Re: [XEN PATCH] tools/console/daemon: fix log_dir memory leak in xenconsoled
Posted by Jan Beulich 1 week, 6 days ago
On 13.08.2026 23:27, Andrew Mbugua wrote:
> Valgrind reports that 21 bytes are "still reachable" from the (XEN_LOG_DIR "/console") allocation:
> 
> HEAP SUMMARY:
>     in use at exit: 21 bytes in 1 blocks
>     total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated
> 
> Since the dynamic memory allocation for the default log directory path happens before the process
> forks into the background, the parent and intermediate processes exit during daemonize()
> with the memory still reachable.
> 
> Move the strdup() allocation down below the daemonize() block. This ensures only the final
> background daemon allocates the default path, matching the lifetime of the free(log_dir)
> cleanup loop at the exit of main().
> 
> With this change, Valgrind reports a clean heap summary
> 
> HEAP SUMMARY:
>     in use at exit: 0 bytes in 0 blocks
>     total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated
> 
> Signed-off-by: Andrew Mbugua <andrewprecious388@gmail.com>

Looks all plausible (albeit a little unnecessary, as memory is freed at
program exit anyway), except that ...

> --- a/tools/console/daemon/main.c
> +++ b/tools/console/daemon/main.c
> @@ -181,10 +181,6 @@ int main(int argc, char **argv)
>  		}
>  	}
>  
> -	if (!log_dir) {
> -		log_dir = strdup(XEN_LOG_DIR "/console");
> -	}
> -
>  	if (geteuid() != 0) {
>  		fprintf(stderr, "%s requires root to run.\n", argv[0]);
>  		exit(EPERM);
> @@ -201,6 +197,10 @@ int main(int argc, char **argv)
>  		daemonize(pidfile ? pidfile : XEN_RUN_DIR "/xenconsoled.pid");
>  	}
>  
> +	if (!log_dir) {
> +                log_dir = strdup(XEN_LOG_DIR "/console");
> +        }
... can you please not screw up indentation? All you want is to move the
code, without converting tabs to blanks.

Jan
Re: [XEN PATCH] tools/console/daemon: fix log_dir memory leak in xenconsoled
Posted by Andrew Precious 1 week, 6 days ago
Noted,it's my 1st first time contributing.

Should I generate a version 2 patch for this?

On Fri, Aug 14, 2026 at 10:52 AM Jan Beulich <jbeulich@suse.com> wrote:

> On 13.08.2026 23:27, Andrew Mbugua wrote:
> > Valgrind reports that 21 bytes are "still reachable" from the
> (XEN_LOG_DIR "/console") allocation:
> >
> > HEAP SUMMARY:
> >     in use at exit: 21 bytes in 1 blocks
> >     total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated
> >
> > Since the dynamic memory allocation for the default log directory path
> happens before the process
> > forks into the background, the parent and intermediate processes exit
> during daemonize()
> > with the memory still reachable.
> >
> > Move the strdup() allocation down below the daemonize() block. This
> ensures only the final
> > background daemon allocates the default path, matching the lifetime of
> the free(log_dir)
> > cleanup loop at the exit of main().
> >
> > With this change, Valgrind reports a clean heap summary
> >
> > HEAP SUMMARY:
> >     in use at exit: 0 bytes in 0 blocks
> >     total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated
> >
> > Signed-off-by: Andrew Mbugua <andrewprecious388@gmail.com>
>
> Looks all plausible (albeit a little unnecessary, as memory is freed at
> program exit anyway), except that ...
>
> > --- a/tools/console/daemon/main.c
> > +++ b/tools/console/daemon/main.c
> > @@ -181,10 +181,6 @@ int main(int argc, char **argv)
> >               }
> >       }
> >
> > -     if (!log_dir) {
> > -             log_dir = strdup(XEN_LOG_DIR "/console");
> > -     }
> > -
> >       if (geteuid() != 0) {
> >               fprintf(stderr, "%s requires root to run.\n", argv[0]);
> >               exit(EPERM);
> > @@ -201,6 +197,10 @@ int main(int argc, char **argv)
> >               daemonize(pidfile ? pidfile : XEN_RUN_DIR
> "/xenconsoled.pid");
> >       }
> >
> > +     if (!log_dir) {
> > +                log_dir = strdup(XEN_LOG_DIR "/console");
> > +        }
> ... can you please not screw up indentation? All you want is to move the
> code, without converting tabs to blanks.
>
> Jan
>
Re: [XEN PATCH] tools/console/daemon: fix log_dir memory leak in xenconsoled
Posted by Jan Beulich 1 week, 6 days ago
On 14.08.2026 09:59, Andrew Precious wrote:
> Noted,it's my 1st first time contributing.
> 
> Should I generate a version 2 patch for this?

May not be necessary, the edit may be possible to do by the committer. Best
wait for the maintainer to provide feedback.

Jan