aboutsummaryrefslogtreecommitdiffstats
path: root/include/asterisk
diff options
context:
space:
mode:
authorrizzo <rizzo@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-18 16:24:44 +0000
committerrizzo <rizzo@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-18 16:24:44 +0000
commit8c2d401961fd9f22c7d6ba4b34e72974f0b241df (patch)
tree62219bf3af0edfa44d744cb06175e5c933ea1191 /include/asterisk
parentd78959e7456c504b505700d985d7b4d4abb8010a (diff)
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: http://svn.digium.com/svn/asterisk/trunk@48560 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'include/asterisk')
-rw-r--r--include/asterisk/strings.h36
1 files changed, 30 insertions, 6 deletions
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 832a964ca..f79e9b2fe 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) {