* src/util/util.h (virVasprintf): New declaration.
* src/util/util.c (virVasprintf): New function.
(virAsprintf): Use it.
* src/util/virtaudit.c (virAuditSend): Likewise.
* src/libvirt_private.syms: Export it.
* cfg.mk (sc_prohibit_asprintf): Also prohibit vasprintf.
* .x-sc_prohibit_asprintf: Add exemption.
---
v2: new patch; makes virCommandAddArgFormat possible in later patch
.x-sc_prohibit_asprintf | 4 +++-
cfg.mk | 2 +-
src/libvirt_private.syms | 1 +
src/util/util.c | 21 +++++++++++++++++----
src/util/util.h | 6 +++++-
src/util/virtaudit.c | 2 +-
6 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/.x-sc_prohibit_asprintf b/.x-sc_prohibit_asprintf
index 614c238..d03b947 100644
--- a/.x-sc_prohibit_asprintf
+++ b/.x-sc_prohibit_asprintf
@@ -1,3 +1,5 @@
+ChangeLog
+^bootstrap.conf$
^gnulib/
^po/
-ChangeLog
+^src/util/util.c$
diff --git a/cfg.mk b/cfg.mk
index 0851f44..dea8301 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -242,7 +242,7 @@ sc_prohibit_strncmp:
# Use virAsprintf rather than as'printf since *strp is undefined on error.
sc_prohibit_asprintf:
- @prohibit='\<a[s]printf\>' \
+ @prohibit='\<v?a[s]printf\>' \
halt='use virAsprintf, not as'printf \
$(_sc_search_regexp)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 49b9be4..d9f70a7 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -812,6 +812,7 @@ virStrToLong_ull;
virStrcpy;
virStrncpy;
virTimestamp;
+virVasprintf;
# uuid.h
diff --git a/src/util/util.c b/src/util/util.c
index a2582aa..3a27c23 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -2197,6 +2197,22 @@ virParseVersionString(const char *str, unsigned long *version)
}
/**
+ * virVasprintf
+ *
+ * like glibc's vasprintf but makes sure *strp == NULL on failure
+ */
+int
+virVasprintf(char **strp, const char *fmt, va_list list)
+{
+ int ret;
+
+ if ((ret = vasprintf(strp, fmt, list)) == -1)
+ *strp = NULL;
+
+ return ret;
+}
+
+/**
* virAsprintf
*
* like glibc's_asprintf but makes sure *strp == NULL on failure
@@ -2208,10 +2224,7 @@ virAsprintf(char **strp, const char *fmt, ...)
int ret;
va_start(ap, fmt);
-
- if ((ret = vasprintf(strp, fmt, ap)) == -1)
- *strp = NULL;
-
+ ret = virVasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
diff --git a/src/util/util.h b/src/util/util.h
index a240d87..edbf01e 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -31,6 +31,7 @@
# include <unistd.h>
# include <sys/select.h>
# include <sys/types.h>
+# include <stdarg.h>
# ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -202,7 +203,10 @@ int virMacAddrCompare (const char *mac1, const char *mac2);
void virSkipSpaces(const char **str);
int virParseNumber(const char **str);
int virParseVersionString(const char *str, unsigned long *version);
-int virAsprintf(char **strp, const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(2, 3);
+int virAsprintf(char **strp, const char *fmt, ...)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3);
+int virVasprintf(char **strp, const char *fmt, va_list list)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0);
char *virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
ATTRIBUTE_RETURN_CHECK;
char *virStrcpy(char *dest, const char *src, size_t destbytes)
diff --git a/src/util/virtaudit.c b/src/util/virtaudit.c
index b630fce..e6bd07f 100644
--- a/src/util/virtaudit.c
+++ b/src/util/virtaudit.c
@@ -94,7 +94,7 @@ void virAuditSend(const char *file ATTRIBUTE_UNUSED, const char *func,
#endif
va_start(args, fmt);
- if (vasprintf(&str, fmt, args) < 0) {
+ if (virVasprintf(&str, fmt, args) < 0) {
VIR_WARN0("Out of memory while formatting audit message");
- str = NULL;
}
--
1.7.3.2