diff --git a/.x-sc_prohibit_asprintf b/.x-sc_prohibit_asprintf index 614c238b3a..d03b947b01 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 5576ecb8c5..6e474c4e74 100644 --- a/cfg.mk +++ b/cfg.mk @@ -255,7 +255,7 @@ sc_prohibit_strncmp: # Use virAsprintf rather than as'printf since *strp is undefined on error. sc_prohibit_asprintf: - @prohibit='\' \ + @prohibit='\' \ halt='use virAsprintf, not as'printf \ $(_sc_search_regexp) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f251c94345..a21928a5a2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -817,6 +817,7 @@ virStrToLong_ull; virStrcpy; virStrncpy; virTimestamp; +virVasprintf; # uuid.h diff --git a/src/util/util.c b/src/util/util.c index 7b8bf4ab64..f6050dee21 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -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; } diff --git a/src/util/util.h b/src/util/util.h index 0385fe18ef..deaf8bbc25 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -31,6 +31,7 @@ # include # include # include +# include # 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 33be4d9676..de7b357d3a 100644 --- a/src/util/virtaudit.c +++ b/src/util/virtaudit.c @@ -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; }