diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 4e279b72df1..ca185909997 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -325,6 +325,20 @@ _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val); */ _PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n); +/** + * @brief Build up a string buffer, handle allocation failure + * + * @param[in] ps Pointer to the talloc'ed string to be extended + * @param[in] fmt The format string + * @param[in] ... The parameters used to fill fmt. + * + * This does nothing if *ps is NULL and sets *ps to NULL if the + * intermediate reallocation fails. Useful when building up a string + * step by step, no intermediate NULL checks are required. + */ + +_PUBLIC_ void talloc_asprintf_addbuf(char **ps, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); + /** Do a case-insensitive, whitespace-ignoring string compare. **/ diff --git a/lib/util/util_str.c b/lib/util/util_str.c index da1989f80f2..b5ba3fb716b 100644 --- a/lib/util/util_str.c +++ b/lib/util/util_str.c @@ -316,3 +316,26 @@ _PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n) return sum != 0; } + +_PUBLIC_ void talloc_asprintf_addbuf(char **ps, const char *fmt, ...) +{ + va_list ap; + char *s = *ps; + char *t = NULL; + + if (s == NULL) { + return; + } + + va_start(ap, fmt); + t = talloc_vasprintf_append_buffer(s, fmt, ap); + va_end(ap); + + if (t == NULL) { + /* signal failure to the next caller */ + TALLOC_FREE(s); + *ps = NULL; + } else { + *ps = t; + } +}