[PATCH 6/6] vsh: Don't crash when @text is NULL in vshCompleterFilter()

Michal Privoznik posted 6 patches 3 months, 3 weeks ago
[PATCH 6/6] vsh: Don't crash when @text is NULL in vshCompleterFilter()
Posted by Michal Privoznik 3 months, 3 weeks ago
This can happen only for cmdComplete() in interactive mode (which
I'm still not convinced is any useful for users and whether we
should support it). Anyway, running plain 'complete' command with
no additional arguments boils down to @text being NULL in
vshReadlineParse() which handles the case just right but is then
subsequently passed to vshCompleterFilter() which isn't prepared
for this case.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/vsh.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index c91d756885..6cc1f60d87 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -2820,7 +2820,7 @@ vshCompleterFilter(char ***list,
     newList = g_new0(char *, list_len + 1);
 
     for (i = 0; i < list_len; i++) {
-        if (!STRPREFIX((*list)[i], text)) {
+        if (text && !STRPREFIX((*list)[i], text)) {
             g_clear_pointer(&(*list)[i], g_free);
             continue;
         }
-- 
2.44.1
Re: [PATCH 6/6] vsh: Don't crash when @text is NULL in vshCompleterFilter()
Posted by Peter Krempa 3 months, 3 weeks ago
On Mon, May 27, 2024 at 11:18:54 +0200, Michal Privoznik wrote:
> This can happen only for cmdComplete() in interactive mode (which
> I'm still not convinced is any useful for users and whether we
> should support it).

Definitely not useful for any normal user. Came in handy when testing
stuff.

But this happpens also in non-interactive mode:

 $ ./tools/virsh complete
 Segmentation fault (core dumped)

> Anyway, running plain 'complete' command with
> no additional arguments boils down to @text being NULL in
> vshReadlineParse() which handles the case just right but is then
> subsequently passed to vshCompleterFilter() which isn't prepared
> for this case.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/vsh.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/vsh.c b/tools/vsh.c
> index c91d756885..6cc1f60d87 100644
> --- a/tools/vsh.c
> +++ b/tools/vsh.c
> @@ -2820,7 +2820,7 @@ vshCompleterFilter(char ***list,
>      newList = g_new0(char *, list_len + 1);
>  
>      for (i = 0; i < list_len; i++) {
> -        if (!STRPREFIX((*list)[i], text)) {
> +        if (text && !STRPREFIX((*list)[i], text)) {
>              g_clear_pointer(&(*list)[i], g_free);
>              continue;

Reviewed-by: Peter Krempa <pkrempa@redhat.com>