summaryrefslogtreecommitdiffstats
path: root/lib/vty.c
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2018-05-29 19:13:51 +0200
committerQuentin Young <qlyoung@cumulusnetworks.com>2018-06-06 18:16:12 +0200
commit5d806ec6e0bce5f1cd32b0d5386c1ad97c31c1f8 (patch)
treec025730971c6c8eb3351b439659a8a36b0e0369d /lib/vty.c
parentlib: add vector_remove() to vector.[ch] (diff)
downloadfrr-5d806ec6e0bce5f1cd32b0d5386c1ad97c31c1f8.tar.xz
frr-5d806ec6e0bce5f1cd32b0d5386c1ad97c31c1f8.zip
lib: fix static analysis issues, use regfree()
* Fix potential NULL dereference * Fix use of uninitialized value * Fix leaking memory by not freeing regex_t * Fix extra \n when using empty regex filter * Clean up still-reachable hook memory * Handle nonexistent pager Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/vty.c')
-rw-r--r--lib/vty.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/vty.c b/lib/vty.c
index 0bdcbf228..803b1c408 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -119,8 +119,8 @@ bool vty_set_include(struct vty *vty, const char *regexp)
bool ret = true;
char errbuf[256];
- if (!regexp) {
- memset(&vty->include, 0x00, sizeof(vty->include));
+ if (!regexp && vty->filter) {
+ regfree(&vty->include);
vty->filter = false;
return true;
}
@@ -188,8 +188,26 @@ int vty_out(struct vty *vty, const char *format, ...)
vector lines = frrstr_split_vec(buf, "\n");
frrstr_filter_vec(lines, &vty->include);
- if (buf[strlen(buf) - 1] == '\n' && vector_active(lines) > 0)
+
+ /*
+ * Consider the string "foo\n". If the regex is an empty string
+ * and the line ended with a newline, then the vector will look
+ * like:
+ *
+ * [0]: 'foo'
+ * [1]: ''
+ *
+ * If the regex isn't empty, the vector will look like:
+ *
+ * [0]: 'foo'
+ *
+ * In this case we'd like to preserve the newline, so we add
+ * the empty string [1] as in the first example.
+ */
+ if (buf[strlen(buf) - 1] == '\n' && vector_active(lines) > 0
+ && strlen(vector_slot(lines, vector_active(lines) - 1)))
vector_set(lines, XSTRDUP(MTYPE_TMP, ""));
+
filtered = frrstr_join_vec(lines, "\n");
frrstr_strvec_free(lines);
} else {