From 6df43392d8703df6e92b7ae6e96373e6c3fc5083 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Mon, 30 Nov 2020 18:50:51 +0300 Subject: [PATCH] vtysh: fix incorrect memory statistics As code comment states, 1 count of MTYPE_COMPLETION is leaked for each autocompleted token. Let's manually decrement the counter before passing the pointer to readline. Signed-off-by: Igor Ryzhov --- lib/memory.c | 6 ++++++ lib/memory.h | 3 +++ vtysh/vtysh.c | 8 +++----- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/memory.c b/lib/memory.c index f715044ea3..a377d3b945 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -127,6 +127,12 @@ void *qstrdup(struct memtype *mt, const char *str) return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL; } +void qcountfree(struct memtype *mt, void *ptr) +{ + if (ptr) + mt_count_free(mt, ptr); +} + void qfree(struct memtype *mt, void *ptr) { if (ptr) diff --git a/lib/memory.h b/lib/memory.h index 13f2f9b11a..e9db12fce2 100644 --- a/lib/memory.h +++ b/lib/memory.h @@ -162,12 +162,15 @@ extern void *qrealloc(struct memtype *mt, void *ptr, size_t size) __attribute__((_ALLOC_SIZE(3), nonnull(1) _RET_NONNULL)); extern void *qstrdup(struct memtype *mt, const char *str) __attribute__((malloc, nonnull(1) _RET_NONNULL)); +extern void qcountfree(struct memtype *mt, void *ptr) + __attribute__((nonnull(1))); extern void qfree(struct memtype *mt, void *ptr) __attribute__((nonnull(1))); #define XMALLOC(mtype, size) qmalloc(mtype, size) #define XCALLOC(mtype, size) qcalloc(mtype, size) #define XREALLOC(mtype, ptr, size) qrealloc(mtype, ptr, size) #define XSTRDUP(mtype, str) qstrdup(mtype, str) +#define XCOUNTFREE(mtype, ptr) qcountfree(mtype, ptr) #define XFREE(mtype, ptr) \ do { \ qfree(mtype, ptr); \ diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index ace4139551..ef0dfccba9 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -1144,12 +1144,10 @@ static char *command_generator(const char *text, int state) cmd_free_strvec(vline); } - if (matched && matched[index]) - /* - * this is free()'d by readline, but we leak 1 count of - * MTYPE_COMPLETION - */ + if (matched && matched[index]) { + XCOUNTFREE(MTYPE_COMPLETION, matched[index]); return matched[index++]; + } XFREE(MTYPE_TMP, matched); -- 2.39.5