From 23be2c0bf4efc42aa0e19a739dca0122b096a4aa Mon Sep 17 00:00:00 2001
From: Luigi Rizzo <rizzo@icir.org>
Date: Mon, 18 Dec 2006 16:24:44 +0000
Subject: [PATCH] apply the proposed fix for bug 8602
 http://bugs.digium.com/view.php?id=8602 (i am not sure if there is still
 missing cast in front of the alloca() call - being a macro this is probably
 triggered only when actually used).

Add function ast_str_reset() to reinitialize the
string to an empty string instead of playing with
the internal fields.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@48560 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 include/asterisk/strings.h | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 832a964ca6..f79e9b2fe7 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -176,6 +176,8 @@ void ast_copy_string(char *dst, const char *src, size_t size),
 /*!
   \brief Build a string in a buffer, designed to be called repeatedly
   
+  \note This method is not recommended. New code should use ast_str_*() instead.
+
   This is a wrapper for snprintf, that properly handles the buffer pointer
   and buffer space available.
 
@@ -283,13 +285,19 @@ struct ast_realloca {
  *
  * Finally, the string can be manipulated with the following:
  *
- *	ast_str_set(&buf, max_len, ts, fmt, ...)
- *	ast_str_append(&buf, max_len, ts, fmt, ...)
+ *	ast_str_set(&buf, max_len, fmt, ...)
+ *	ast_str_append(&buf, max_len, fmt, ...)
+ *
+ * and their varargs variant
  *
- * and their varargs format.
+ *	ast_str_set_va(&buf, max_len, ap)
+ *	ast_str_append_va(&buf, max_len, ap)
  *
  * \arg max_len The maximum allowed length, reallocating if needed.
  * 	0 means unlimited, -1 means "at most the available space"
+ *
+ * \return All the functions return <0 in case of error, or the
+ *	length of the string added to the buffer otherwise.
  */
 
 /*! \brief The descriptor of a dynamic string
@@ -325,7 +333,8 @@ struct ast_str * attribute_malloc ast_str_create(size_t init_len),
 {
 	struct ast_str *buf;
 
-	if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
+	buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
+	if (buf == NULL)
 		return NULL;
 	
 	buf->len = init_len;
@@ -336,6 +345,20 @@ struct ast_str * attribute_malloc ast_str_create(size_t init_len),
 }
 )
 
+/*! \brief Reset the content of a dynamic string.
+ * Useful before a series of ast_str_append.
+ */
+AST_INLINE_API(
+void ast_str_reset(struct ast_str *buf),
+{
+	if (buf) {
+		buf->used = 0;
+		if (buf->len)
+			buf->str[0] = '\0';
+	}
+}
+)
+
 /*!
  * Make space in a new string (e.g. to read in data from a file)
  */
@@ -346,7 +369,7 @@ int ast_str_make_space(struct ast_str **buf, size_t new_len),
 		return 0;	/* success */
 	if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
 		return -1;	/* cannot extend */
-	*buf = ast_realloc(*buf, new_len + sizeof(struct ast_str));
+	*buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
 	if (*buf == NULL) /* XXX watch out, we leak memory here */
 		return -1;
 	if ((*buf)->ts != DS_MALLOC)
@@ -406,7 +429,8 @@ struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
 {
 	struct ast_str *buf;
 
-	if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
+	buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
+	if (buf == NULL)
 		return NULL;
 	
 	if (!buf->len) {
-- 
GitLab