]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: use snprintfrr() in "hidden" printfs
authorDavid Lamparter <equinox@diac24.net>
Wed, 12 Jun 2019 15:11:21 +0000 (17:11 +0200)
committerDavid Lamparter <equinox@diac24.net>
Wed, 12 Jun 2019 17:35:43 +0000 (19:35 +0200)
We need to be calling snprintfrr() instead of snprintf() in places that
wrap snprintf in some user-exposed way; otherwise the extensions won't
be available for those functions.

Signed-off-by: David Lamparter <equinox@diac24.net>
isisd/isis_misc.c
lib/sbuf.c
lib/termtable.c
lib/vty.c

index 0a42adea37ba4aade5673025b52bd10f1e02cac1..d4c38efaf3016ed1c8f0a36758ba79262b0d5be9 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <zebra.h>
 
+#include "printfrr.h"
 #include "stream.h"
 #include "vty.h"
 #include "hash.h"
@@ -511,42 +512,14 @@ void zlog_dump_data(void *data, int len)
        return;
 }
 
-static char *qasprintf(const char *format, va_list ap)
-{
-       va_list aq;
-       va_copy(aq, ap);
-
-       int size = 0;
-       char *p = NULL;
-
-       size = vsnprintf(p, size, format, ap);
-
-       if (size < 0) {
-               va_end(aq);
-               return NULL;
-       }
-
-       size++;
-       p = XMALLOC(MTYPE_TMP, size);
-
-       size = vsnprintf(p, size, format, aq);
-       va_end(aq);
-
-       if (size < 0) {
-               XFREE(MTYPE_TMP, p);
-               return NULL;
-       }
-
-       return p;
-}
-
 void log_multiline(int priority, const char *prefix, const char *format, ...)
 {
+       char shortbuf[256];
        va_list ap;
        char *p;
 
        va_start(ap, format);
-       p = qasprintf(format, ap);
+       p = asnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
        va_end(ap);
 
        if (!p)
@@ -558,16 +531,18 @@ void log_multiline(int priority, const char *prefix, const char *format, ...)
                zlog(priority, "%s%s", prefix, line);
        }
 
-       XFREE(MTYPE_TMP, p);
+       if (p != shortbuf)
+               XFREE(MTYPE_TMP, p);
 }
 
 void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
 {
+       char shortbuf[256];
        va_list ap;
        char *p;
 
        va_start(ap, format);
-       p = qasprintf(format, ap);
+       p = asnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
        va_end(ap);
 
        if (!p)
@@ -579,7 +554,8 @@ void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
                vty_out(vty, "%s%s\n", prefix, line);
        }
 
-       XFREE(MTYPE_TMP, p);
+       if (p != shortbuf)
+               XFREE(MTYPE_TMP, p);
 }
 
 void vty_out_timestr(struct vty *vty, time_t uptime)
index 03a2be3e09fa39dc0ea6ac9405b679a64f422afc..c04af153b19c01f365219cf89b81420bddbb2055 100644 (file)
@@ -22,6 +22,7 @@
  */
 #include <zebra.h>
 
+#include "printfrr.h"
 #include "sbuf.h"
 #include "memory.h"
 
@@ -68,7 +69,7 @@ void sbuf_push(struct sbuf *buf, int indent, const char *format, ...)
 
                written1 = indent;
                va_start(args, format);
-               written2 = vsnprintf(NULL, 0, format, args);
+               written2 = vsnprintfrr(NULL, 0, format, args);
                va_end(args);
 
                new_size = buf->size;
@@ -92,8 +93,8 @@ void sbuf_push(struct sbuf *buf, int indent, const char *format, ...)
                buf->pos = buf->size;
 
        va_start(args, format);
-       written = vsnprintf(buf->buf + buf->pos, buf->size - buf->pos, format,
-                           args);
+       written = vsnprintfrr(buf->buf + buf->pos, buf->size - buf->pos,
+                             format, args);
        va_end(args);
 
        if (written >= 0)
index 01468b820363645e746a3f0ef53753f04b523941..b59c1118f8ab17597777ec4f7016fb8b77247f5f 100644 (file)
@@ -20,6 +20,7 @@
 #include <zebra.h>
 #include <stdio.h>
 
+#include "printfrr.h"
 #include "memory.h"
 #include "termtable.h"
 
@@ -134,6 +135,7 @@ static struct ttable_cell *ttable_insert_row_va(struct ttable *tt, int i,
 {
        assert(i >= -1 && i < tt->nrows);
 
+       char shortbuf[256];
        char *res, *orig, *section;
        struct ttable_cell *row;
        int col = 0;
@@ -158,9 +160,7 @@ static struct ttable_cell *ttable_insert_row_va(struct ttable *tt, int i,
        /* CALLOC a block of cells */
        row = XCALLOC(MTYPE_TTABLE, tt->ncols * sizeof(struct ttable_cell));
 
-       res = NULL;
-       vasprintf(&res, format, ap);
-
+       res = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
        orig = res;
 
        while (res && col < tt->ncols) {
@@ -170,7 +170,8 @@ static struct ttable_cell *ttable_insert_row_va(struct ttable *tt, int i,
                col++;
        }
 
-       free(orig);
+       if (orig != shortbuf)
+               XFREE(MTYPE_TMP, orig);
 
        /* insert row */
        if (i == -1 || i == tt->nrows)
index e306e69b873c4f42e8558cdf9bd90f28207078be..fee585d5cb7158199589270e77bf41680f99b474 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -105,8 +105,8 @@ void vty_frame(struct vty *vty, const char *format, ...)
        va_list args;
 
        va_start(args, format);
-       vsnprintf(vty->frame + vty->frame_pos,
-                 sizeof(vty->frame) - vty->frame_pos, format, args);
+       vsnprintfrr(vty->frame + vty->frame_pos,
+                   sizeof(vty->frame) - vty->frame_pos, format, args);
        vty->frame_pos = strlen(vty->frame);
        va_end(args);
 }