From 039dc61292de5f3ed5f46316b1940ab6bb184c3f Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Fri, 9 Sep 2016 21:58:33 +0000 Subject: [PATCH] lib: Fix tab completions memleak, memory stats corruption Reviewed-by: Donald Sharp Signed-off-by: Quentin Young --- lib/command.c | 14 ++++++++++---- vtysh/vtysh.c | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/command.c b/lib/command.c index f97e37e8c6..e67007ae2f 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2416,8 +2416,11 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status, int islib /* Only one matched */ if (vector_slot (matchvec, 1) == NULL) { - match_str = (char **) matchvec->index; - vector_only_wrapper_free (matchvec); + size_t index_size = matchvec->alloced * sizeof (void *); + match_str = XMALLOC (MTYPE_TMP, index_size); + memcpy (match_str, matchvec->index, index_size); + vector_free (matchvec); + *status = CMD_COMPLETE_FULL_MATCH; return match_str; } @@ -2459,8 +2462,11 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status, int islib /* Make new matchvec. */ matchvec = vector_init (INIT_MATCHVEC_SIZE); vector_set (matchvec, lcdstr); - match_str = (char **) matchvec->index; - vector_only_wrapper_free (matchvec); + + size_t index_size = matchvec->alloced * sizeof (void *); + match_str = XMALLOC (MTYPE_TMP, index_size); + memcpy (match_str, matchvec->index, index_size); + vector_free (matchvec); *status = CMD_COMPLETE_MATCH; return match_str; diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 2436c6182d..d01a1bcebb 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -885,7 +885,10 @@ command_generator (const char *text, int state) if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1])) vector_set (vline, NULL); + if (matched) + XFREE (MTYPE_TMP, matched); matched = cmd_complete_command (vline, vty, &complete_status); + cmd_free_strvec (vline); } if (matched && matched[index]) -- 2.39.5