]> git.proxmox.com Git - mirror_frr.git/commitdiff
vtysh: fix incorrect memory statistics
authorIgor Ryzhov <iryzhov@nfware.com>
Mon, 30 Nov 2020 15:50:51 +0000 (18:50 +0300)
committerIgor Ryzhov <iryzhov@nfware.com>
Mon, 30 Nov 2020 15:55:40 +0000 (18:55 +0300)
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 <iryzhov@nfware.com>
lib/memory.c
lib/memory.h
vtysh/vtysh.c

index f715044ea302c2ef6766be310518c4072830d4e2..a377d3b945e0047b36ce5ad1825acb8d11c590ae 100644 (file)
@@ -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)
index 13f2f9b11ac06dcc7a59d26d75760e275ed90c6e..e9db12fce2ca85738165df3a7c9b4e0fba6f3d03 100644 (file)
@@ -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);                                             \
index ace4139551cb9b6ccc4f0ea15f9fce1bd1fc5dac..ef0dfccba91f50fb44931a52edf3041d4ee09f8a 100644 (file)
@@ -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);