1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

lib: Add talloc_asprintf_addbuf()

Simplifies building up a string step by step, see next commit

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Volker Lendecke 2021-10-06 09:53:57 +02:00 committed by Jeremy Allison
parent 55ec7e6d00
commit 20536080a5
2 changed files with 37 additions and 0 deletions

View File

@ -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.
**/

View File

@ -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;
}
}