[PATCH 0/2] cmdComplete: Fix two memleaks

Putting my recently-reviewed series through CI revealed that there were two pre-existing memleaks in the completion code caught by the test additions. Peter Krempa (2): vsh: cmdComplete: Don't leak buffer for completion vshReadlineInit: Initialize only once tools/vsh.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) -- 2.44.0

The buffer which we assign to the 'rl_line_buffer' variable of readline would be overwritten and thus leaked on multiple invocations of cmdComplete in one session. Free/clear it after it's used. Hitting this leak was until recenly possible only in non-interactive batch mode and recently also in interactive mode as 'complete' can be used multiple times now interactively. Fixes: a0e1ada63c0afdc2af3b9405cbf637d8bd28700c Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/vsh.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/vsh.c b/tools/vsh.c index 2805574ec6..05de54b5b0 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3439,7 +3439,10 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd) * In our case it's at the end of the whole line. */ rl_point = strlen(rl_line_buffer); - if (!(matches = vshReadlineCompletion(arg, 0, 0))) + matches = vshReadlineCompletion(arg, 0, 0); + g_clear_pointer(&rl_line_buffer, g_free); + + if (!matches) return false; for (iter = matches; *iter; iter++) { -- 2.44.0

'vshReadlineInit' is called when interactive virsh is started but also on each call to 'cmdComplete'. Calling it repeatedly (using the 'complete' command interactively, or multiple times in batch mode) leaks the buffers for history file configuration. Avoid multiple setups of this function by returning success in case the history file config is already present. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/vsh.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/vsh.c b/tools/vsh.c index 05de54b5b0..f954f7af77 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2908,6 +2908,10 @@ vshReadlineInit(vshControl *ctl) const char *break_characters = " \t\n`@$><=;|&{("; const char *quote_characters = "\"'"; + /* initialize readline stuff only once */ + if (ctl->historydir) + return 0; + /* Opaque data for autocomplete callbacks. */ autoCompleteOpaque = ctl; -- 2.44.0

On a Thursday in 2024, Peter Krempa wrote:
Putting my recently-reviewed series through CI revealed that there were two pre-existing memleaks in the completion code caught by the test additions.
Peter Krempa (2): vsh: cmdComplete: Don't leak buffer for completion vshReadlineInit: Initialize only once
tools/vsh.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Peter Krempa