]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Add k5_buf_add_vfmt to k5buf interface
authorGreg Hudson <ghudson@mit.edu>
Thu, 4 Jan 2018 19:35:12 +0000 (14:35 -0500)
committerGreg Hudson <ghudson@mit.edu>
Tue, 16 Jan 2018 16:49:13 +0000 (11:49 -0500)
src/include/k5-buf.h
src/util/support/k5buf.c
src/util/support/libkrb5support-fixed.exports

index f3207bd09afdc4a7eaf54925cad940ed293d63dc..1223916a67d48247b8e7a4e8c397e9334f277707 100644 (file)
@@ -76,6 +76,14 @@ void k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
 #endif
     ;
 
+/* Add sprintf-style formatted data to BUF, with a va_list.  The value of ap is
+ * undefined after the call. */
+void k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap)
+#if !defined(__cplusplus) && (__GNUC__ > 2)
+    __attribute__((__format__(__printf__, 2, 0)))
+#endif
+    ;
+
 /* Extend the length of buf by len and return a pointer to the reserved space,
  * to be filled in by the caller.  Return NULL on error. */
 void *k5_buf_get_space(struct k5buf *buf, size_t len);
index f619f6a489ba1880dc626cc75241b9020f5c7251..35978f238c2074fe4dc74d4506aa2f2e9dc04670 100644 (file)
@@ -141,9 +141,9 @@ k5_buf_add_len(struct k5buf *buf, const void *data, size_t len)
 }
 
 void
-k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
+k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap)
 {
-    va_list ap;
+    va_list apcopy;
     int r;
     size_t remaining;
     char *tmp;
@@ -154,9 +154,7 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
 
     if (buf->buftype == K5BUF_FIXED) {
         /* Format the data directly into the fixed buffer. */
-        va_start(ap, fmt);
         r = vsnprintf(endptr(buf), remaining, fmt, ap);
-        va_end(ap);
         if (SNPRINTF_OVERFLOW(r, remaining))
             set_error(buf);
         else
@@ -166,9 +164,9 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
 
     /* Optimistically format the data directly into the dynamic buffer. */
     assert(buf->buftype == K5BUF_DYNAMIC);
-    va_start(ap, fmt);
-    r = vsnprintf(endptr(buf), remaining, fmt, ap);
-    va_end(ap);
+    va_copy(apcopy, ap);
+    r = vsnprintf(endptr(buf), remaining, fmt, apcopy);
+    va_end(apcopy);
     if (!SNPRINTF_OVERFLOW(r, remaining)) {
         buf->len += (unsigned int) r;
         return;
@@ -179,9 +177,7 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
         if (!ensure_space(buf, r))
             return;
         remaining = buf->space - buf->len;
-        va_start(ap, fmt);
         r = vsnprintf(endptr(buf), remaining, fmt, ap);
-        va_end(ap);
         if (SNPRINTF_OVERFLOW(r, remaining))  /* Shouldn't ever happen. */
             k5_buf_free(buf);
         else
@@ -191,9 +187,7 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
 
     /* It's a pre-C99 snprintf implementation, or something else went wrong.
      * Fall back to asprintf. */
-    va_start(ap, fmt);
     r = vasprintf(&tmp, fmt, ap);
-    va_end(ap);
     if (r < 0) {
         k5_buf_free(buf);
         return;
@@ -206,6 +200,16 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
     free(tmp);
 }
 
+void
+k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    k5_buf_add_vfmt(buf, fmt, ap);
+    va_end(ap);
+}
+
 void *
 k5_buf_get_space(struct k5buf *buf, size_t len)
 {
index fd74a1897ebbccc0a314b14fb456e4859e10be35..12dab3885249f57180fb6be4cc2207b149e36ef5 100644 (file)
@@ -6,6 +6,7 @@ k5_buf_init_dynamic
 k5_buf_add
 k5_buf_add_len
 k5_buf_add_fmt
+k5_buf_add_vfmt
 k5_buf_get_space
 k5_buf_truncate
 k5_buf_status