Commit b4bd76bc authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

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: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 407b550f
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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.
 *
+18 −5
Original line number Diff line number Diff line
@@ -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)
{
+1 −1
Original line number Diff line number Diff line
@@ -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