util: add virVasprintf

* 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.
This commit is contained in:
Eric Blake 2010-11-22 16:39:47 -07:00
parent dbab6bb5cf
commit fce3baee26
6 changed files with 28 additions and 8 deletions

View File

@ -1,3 +1,5 @@
ChangeLog
^bootstrap.conf$
^gnulib/
^po/
ChangeLog
^src/util/util.c$

2
cfg.mk
View File

@ -255,7 +255,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)

View File

@ -817,6 +817,7 @@ virStrToLong_ull;
virStrcpy;
virStrncpy;
virTimestamp;
virVasprintf;
# uuid.h

View File

@ -2198,6 +2198,22 @@ virParseVersionString(const char *str, unsigned long *version)
return 0;
}
/**
* 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
*
@ -2210,10 +2226,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;
}

View File

@ -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)

View File

@ -95,7 +95,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;
}