diff options
-rw-r--r-- | src/pulsecore/core-util.c | 20 | ||||
-rw-r--r-- | src/pulsecore/core-util.h | 1 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index df110966..c8ea4f52 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -1592,12 +1592,28 @@ int pa_snprintf(char *str, size_t size, const char *format, ...) { pa_assert(format); va_start(ap, format); - ret = vsnprintf(str, size, format, ap); + ret = pa_vsnprintf(str, size, format, ap); va_end(ap); + return ret; +} + +/* Same as vsnprintf, but guarantees NUL-termination on every platform */ +int pa_vsnprintf(char *str, size_t size, const char *format, va_list ap) { + int ret; + + pa_assert(str); + pa_assert(size > 0); + pa_assert(format); + + ret = vsnprintf(str, size, format, ap); + str[size-1] = 0; - return ret; + if (ret < 0) + ret = strlen(str); + + return PA_MIN((int) size-1, ret); } /* Truncate the specified string, but guarantee that the string diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index e9313d4b..ec4cdc43 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -128,6 +128,7 @@ int pa_atou(const char *s, uint32_t *ret_u); int pa_atof(const char *s, float *ret_f); int pa_snprintf(char *str, size_t size, const char *format, ...); +int pa_vsnprintf(char *str, size_t size, const char *format, va_list ap); char *pa_truncate_utf8(char *c, size_t l); |