From 5b5231b091e69da604d1f28303dcc2cc3fc02976 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 31 Oct 2016 03:34:27 +0000 Subject: [PATCH] lib: Fix segfault on erroneous command Command completion vector should only be copied if it is non-null. Signed-off-by: Quentin Young --- lib/command.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/command.c b/lib/command.c index a12591a8bc..5e493a1c01 100644 --- a/lib/command.c +++ b/lib/command.c @@ -677,20 +677,21 @@ cmd_complete_command (vector vline, struct vty *vty, int *status) vector comps, initial_comps; initial_comps = cmd_complete_command_real (input_line, vty, status); - // filter out everything that is not suitable for a tab-completion - comps = vector_init (VECTOR_MIN_SIZE); - for (unsigned int i = 0; i < vector_active(initial_comps); i++) - { - struct cmd_token *token = vector_slot (initial_comps, i); - if (token->type == WORD_TKN) - vector_set (comps, token); - else - del_cmd_token (token); - } - vector_free (initial_comps); - if (!MATCHER_ERROR (*status)) { + assert (initial_comps); + // filter out everything that is not suitable for a tab-completion + comps = vector_init (VECTOR_MIN_SIZE); + for (unsigned int i = 0; i < vector_active(initial_comps); i++) + { + struct cmd_token *token = vector_slot (initial_comps, i); + if (token->type == WORD_TKN) + vector_set (comps, token); + else + del_cmd_token (token); + } + vector_free (initial_comps); + // copy completions text into an array of char* ret = XMALLOC (MTYPE_TMP, vector_active (comps) * sizeof (char *) + 1); unsigned int i; -- 2.39.5