mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-23 21:19:09 +00:00
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
This commit is contained in:
@@ -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 format.
|
||||
* and their varargs variant
|
||||
*
|
||||
* 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) {
|
||||
|
Reference in New Issue
Block a user