]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Add talloc_asprintf_addbuf()
authorVolker Lendecke <vl@samba.org>
Wed, 6 Oct 2021 07:53:57 +0000 (09:53 +0200)
committerJeremy Allison <jra@samba.org>
Fri, 8 Oct 2021 19:28:31 +0000 (19:28 +0000)
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>
lib/util/samba_util.h
lib/util/util_str.c

index 4e279b72df166bf789433d0b508fb55d65744c6a..ca185909997797e934663f4120f17adfaa2498b4 100644 (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.
 **/
index da1989f80f298f718f8fd7e58a91a2ef49bdca54..b5ba3fb716be43ca52579b7c24a9a463d5165f40 100644 (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;
+       }
+}