diff options
-rw-r--r-- | src/pulsecore/core-util.c | 20 | ||||
-rw-r--r-- | src/pulsecore/core-util.h | 2 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index e5766b2f..98790484 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -31,7 +31,6 @@ #include <stdlib.h> #include <signal.h> #include <errno.h> -#include <assert.h> #include <string.h> #include <stdio.h> #include <fcntl.h> @@ -83,6 +82,7 @@ #include <pulsecore/core-error.h> #include <pulsecore/winsock.h> #include <pulsecore/log.h> +#include <pulsecore/macro.h> #include "core-util.h" @@ -1202,3 +1202,21 @@ int pa_atou(const char *s, uint32_t *ret_u) { return 0; } + +/* Same as snprintf, but guarantees NUL-termination on every platform */ +int pa_snprintf(char *str, size_t size, const char *format, ...) { + int ret; + va_list ap; + + pa_assert(str); + pa_assert(size > 0); + pa_assert(format); + + va_start(ap, format); + ret = vsnprintf(str, size, format, ap); + va_end(ap); + + str[size-1] = 0; + + return ret; +} diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index 1d921e03..a593317d 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -92,4 +92,6 @@ char *pa_runtime_path(const char *fn, char *s, size_t l); int pa_atoi(const char *s, int32_t *ret_i); int pa_atou(const char *s, uint32_t *ret_u); +int pa_snprintf(char *str, size_t size, const char *format, ...); + #endif |