summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2021-03-26 17:58:54 +0100
committerDavid Lamparter <equinox@diac24.net>2021-03-30 22:32:59 +0200
commit2d9a4e2931e4f20c6bc49b09956a1e350664e3f2 (patch)
treee7b0703a18ccc7c4cf657a81de23dc559618c3dd
parentlib: put printfrr extension args into struct (diff)
downloadfrr-2d9a4e2931e4f20c6bc49b09956a1e350664e3f2.tar.xz
frr-2d9a4e2931e4f20c6bc49b09956a1e350664e3f2.zip
lib: allow discerning unspec width in printfrr ext
With 0 currently the default value for the width specifier, it's not possible to discern that from a %*p where 0 was passed as the length parameter. Use -1 to allow for that. Signed-off-by: David Lamparter <equinox@diac24.net>
-rw-r--r--lib/printf/vfprintf.c8
-rw-r--r--lib/printfrr.h17
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/printf/vfprintf.c b/lib/printf/vfprintf.c
index 7fafa89aa..1bd24743e 100644
--- a/lib/printf/vfprintf.c
+++ b/lib/printf/vfprintf.c
@@ -305,7 +305,7 @@ vbprintfrr(struct fbuf *cb_in, const char *fmt0, va_list ap)
flags = 0;
dprec = 0;
- width = 0;
+ width = -1;
prec = -1;
sign = '\0';
ox[1] = '\0';
@@ -688,6 +688,9 @@ number: if ((dprec = prec) >= 0)
* Compute actual size, so we know how much to pad.
* size excludes decimal prec; realsz includes it.
*/
+ if (width < 0)
+ width = 0;
+
realsz = dprec > size ? dprec : size;
if (sign)
realsz++;
@@ -750,6 +753,9 @@ ext_printed:
* Keep analogous to code above please.
*/
+ if (width < 0)
+ width = 0;
+
realsz = size;
prsize = width > realsz ? width : realsz;
if ((unsigned int)ret + prsize > INT_MAX) {
diff --git a/lib/printfrr.h b/lib/printfrr.h
index 754cb0959..9dd20f0a8 100644
--- a/lib/printfrr.h
+++ b/lib/printfrr.h
@@ -178,6 +178,23 @@ struct printfrr_eargs {
bool leftadj;
};
+/* for any extension that needs a buffer length */
+
+static inline ssize_t printfrr_ext_len(struct printfrr_eargs *ea)
+{
+ ssize_t rv;
+
+ if (ea->precision >= 0)
+ rv = ea->precision;
+ else if (ea->width >= 0) {
+ rv = ea->width;
+ ea->width = -1;
+ } else
+ rv = -1;
+
+ return rv;
+}
+
/* no locking - must be called when single threaded (e.g. at startup.)
* this restriction hopefully won't be a huge bother considering normal usage
* scenarios...