util: add vsprintf version of spdk_sprintf_alloc

-Wformat-nonliteral needs to be disabled since clang triggers it on the
call to vsnprintf() now that it is nested two calls deep.

Change-Id: I228b9d099cfc2b65181941cbb4798b7f8eae3baa
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-11-18 10:48:24 -07:00
parent 407b550ff4
commit b4bd76bc63
3 changed files with 29 additions and 6 deletions

View File

@ -42,6 +42,7 @@
extern "C" {
#endif
#include <stdarg.h>
#include <stddef.h>
/**
@ -53,6 +54,15 @@ extern "C" {
*/
char *spdk_sprintf_alloc(const char *format, ...) __attribute__((format(printf, 1, 2)));
/**
* vsprintf with automatic buffer allocation.
*
* The return value is the formatted string,
* which should be passed to free() when no longer needed,
* or NULL on failure.
*/
char *spdk_vsprintf_alloc(const char *format, va_list args);
/**
* Convert string to lowercase in place.
*

View File

@ -42,9 +42,9 @@
#include "spdk/string.h"
char *
spdk_sprintf_alloc(const char *format, ...)
spdk_vsprintf_alloc(const char *format, va_list args)
{
va_list args;
va_list args_copy;
char *buf;
size_t bufsize;
int rc;
@ -59,9 +59,9 @@ spdk_sprintf_alloc(const char *format, ...)
return NULL;
}
va_start(args, format);
rc = vsnprintf(buf, bufsize, format, args);
va_end(args);
va_copy(args_copy, args);
rc = vsnprintf(buf, bufsize, format, args_copy);
va_end(args_copy);
/*
* If vsnprintf() returned a count within our current buffer size, we are done.
@ -85,6 +85,19 @@ spdk_sprintf_alloc(const char *format, ...)
return NULL;
}
char *
spdk_sprintf_alloc(const char *format, ...)
{
va_list args;
char *ret;
va_start(args, format);
ret = spdk_vsprintf_alloc(format, args);
va_end(args);
return ret;
}
char *
spdk_strlwr(char *s)
{

View File

@ -54,7 +54,7 @@ ifeq ($(CONFIG_WERROR), y)
COMMON_CFLAGS += -Werror
endif
COMMON_CFLAGS += -Wformat -Wformat-security -Wformat-nonliteral
COMMON_CFLAGS += -Wformat -Wformat-security
COMMON_CFLAGS += -D_GNU_SOURCE