[PATCH 0/2] vsh: Fix one memleak and const correctness

*** BLURB HERE *** Michal Prívozník (2): vsh: Free commands in vshDeinit() vsh: Turn _vshControl::progname into a const string tools/vsh.c | 1 + tools/vsh.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> Whether virsh/virt-admin is running in interactive or non-interactive mode, vshControl::cmd contains the batch of last executed commands as a linked list. Just look into vshCommandParse(). Free the linked list in vshDeinit() to avoid memleak. 3,312 bytes in 3 blocks are still reachable in loss record 572 of 577 at 0x484CEF3: calloc (vg_replace_malloc.c:1675) by 0x506AB29: g_malloc0 (in /usr/lib64/libglib-2.0.so.0.8200.5) by 0x1B74B8: vshCmdNew (vsh.c:1466) by 0x1B7A80: vshCommandParse (vsh.c:1615) by 0x1B8458: vshCommandStringParse (vsh.c:1874) by 0x1419C1: virshParseArgv (virsh.c:773) by 0x141D11: main (virsh.c:879) Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/vsh.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/vsh.c b/tools/vsh.c index b42e2f0c50..d1e699505b 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3204,6 +3204,7 @@ vshDeinit(vshControl *ctl) * interactive mode. */ vshReadlineDeinit(ctl); vshCloseLogFile(ctl); + g_clear_pointer(&ctl->cmd, vshCommandFree); } /* ----------------------------------------------- -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> The aim of the progname member of the _vshControl struct is to point to argv[0] which is then used in vshOutputLogFile() to create a prefix for a log message. But the member is never modified (nor it should be) and thus can be a const char *. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/vsh.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/vsh.h b/tools/vsh.h index 5970addfb6..8b87c00ff4 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -194,7 +194,7 @@ struct _vshControl { * to program name */ const char *env_prefix; /* hardcoded environment variable prefix */ char *connname; /* connection name */ - char *progname; /* program name */ + const char *progname; /* program name */ vshCmd *cmd; /* the current command */ char *cmdstr; /* string with command */ bool imode; /* interactive mode? */ -- 2.49.0

On Mon, Apr 14, 2025 at 10:10:34 +0200, Michal Privoznik wrote:
*** BLURB HERE ***
Michal Prívozník (2): vsh: Free commands in vshDeinit() vsh: Turn _vshControl::progname into a const string
tools/vsh.c | 1 + tools/vsh.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
participants (2)
-
Jiri Denemark
-
Michal Privoznik