From: Vincent Bernat Date: Fri, 21 Apr 2023 20:31:26 +0000 (+0200) Subject: client: don't output empty lines on configure commands X-Git-Tag: 1.0.17~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=14ce09980a718e054301f9de08d8bc1ffc5656f0;p=thirdparty%2Flldpd.git client: don't output empty lines on configure commands --- diff --git a/NEWS b/NEWS index ffc8e804..6fda933e 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ lldpd (1.0.17) * Fix: + Read overflow when parsing CDP addresses. Thanks to Matteo Memelli. + + Don't output empty lines on configure commands. lldpd (1.0.16) * Fix: diff --git a/src/client/text_writer.c b/src/client/text_writer.c index 46664d09..95990e74 100644 --- a/src/client/text_writer.c +++ b/src/client/text_writer.c @@ -17,6 +17,7 @@ #include #include +#include #include #include "writer.h" @@ -29,8 +30,27 @@ struct txt_writer_private { FILE *fh; int level; int attrs; + int lastnl; }; +static void +txt_fprintf(struct txt_writer_private *p, const char *fmt, ...) +{ + va_list ap; + + // Don't add a newline if we already had one. + while (p->lastnl && fmt[0] == '\n') { + fmt += 1; + } + if (fmt[0] == '\0') return; + + va_start(ap, fmt); + vfprintf(p->fh, fmt, ap); + va_end(ap); + + p->lastnl = (strlen(fmt) > 0 && fmt[strlen(fmt) - 1] == '\n'); +} + static void txt_start(struct writer *w, const char *tag, const char *descr) { @@ -39,19 +59,19 @@ txt_start(struct writer *w, const char *tag, const char *descr) char buf[128]; if (p->level == 0) { - fprintf(p->fh, "%s\n", sep); - } else { - fprintf(p->fh, "\n"); + txt_fprintf(p, "%s\n", sep); + } else if (!p->lastnl) { + txt_fprintf(p, "\n"); } for (i = 1; i < p->level; i++) { - fprintf(p->fh, " "); + txt_fprintf(p, " "); } snprintf(buf, sizeof(buf), "%s:", descr); - fprintf(p->fh, "%-13s", buf); + txt_fprintf(p, "%-13s", buf); - if (p->level == 0) fprintf(p->fh, "\n%s", sep); + if (p->level == 0) txt_fprintf(p, "\n%s", sep); p->level++; p->attrs = 0; @@ -63,10 +83,10 @@ txt_attr(struct writer *w, const char *tag, const char *descr, const char *value struct txt_writer_private *p = w->priv; if (descr == NULL || strlen(descr) == 0) { - fprintf(p->fh, "%s%s", (p->attrs > 0 ? ", " : " "), + txt_fprintf(p, "%s%s", (p->attrs > 0 ? ", " : " "), value ? value : "(none)"); } else { - fprintf(p->fh, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr, + txt_fprintf(p, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr, value ? value : "(none)"); } @@ -81,24 +101,24 @@ txt_data(struct writer *w, const char *data) char *v = begin = data ? strdup(data) : NULL; if (v == NULL) { - fprintf(p->fh, " %s", data ? data : "(none)"); + txt_fprintf(p, " %s", data ? data : "(none)"); return; } - fprintf(p->fh, " "); + txt_fprintf(p, " "); while ((nl = strchr(v, '\n')) != NULL) { *nl = '\0'; - fprintf(p->fh, "%s\n", v); + txt_fprintf(p, "%s\n", v); v = nl + 1; /* Indent */ int i; for (i = 1; i < p->level - 1; i++) { - fprintf(p->fh, " "); + txt_fprintf(p, " "); } - fprintf(p->fh, "%-14s", " "); + txt_fprintf(p, "%-14s", " "); } - fprintf(p->fh, "%s", v); + txt_fprintf(p, "%s", v); free(begin); } @@ -109,7 +129,7 @@ txt_end(struct writer *w) p->level--; if (p->level == 1) { - fprintf(p->fh, "\n%s", sep); + txt_fprintf(p, "\n%s", sep); fflush(p->fh); } } @@ -119,7 +139,7 @@ txt_finish(struct writer *w) { struct txt_writer_private *p = w->priv; - fprintf(p->fh, "\n"); + txt_fprintf(p, "\n"); free(w->priv); w->priv = NULL; @@ -143,6 +163,7 @@ txt_init(FILE *fh) priv->fh = fh; priv->level = 0; priv->attrs = 0; + priv->lastnl = 1; result = malloc(sizeof(struct writer)); if (!result) {