Add va_end calls to __ast_str_helper.
[asterisk/asterisk.git] / main / strings.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief String manipulation API
22  *
23  * \author Tilghman Lesher <tilghman@digium.com>
24  */
25
26 /*** MAKEOPTS
27 <category name="MENUSELECT_CFLAGS" displayname="Compiler Flags" positive_output="yes">
28         <member name="DEBUG_OPAQUE" displayname="Change ast_str internals to detect improper usage" touch_on_change="include/asterisk/strings.h">
29                 <defaultenabled>yes</defaultenabled>
30         </member>
31 </category>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/strings.h"
39 #include "asterisk/pbx.h"
40
41 /*!
42  * core handler for dynamic strings.
43  * This is not meant to be called directly, but rather through the
44  * various wrapper macros
45  *      ast_str_set(...)
46  *      ast_str_append(...)
47  *      ast_str_set_va(...)
48  *      ast_str_append_va(...)
49  */
50
51 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
52 int __ast_debug_str_helper(struct ast_str **buf, size_t max_len,
53         int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
54 #else
55 int __ast_str_helper(struct ast_str **buf, size_t max_len,
56         int append, const char *fmt, va_list ap)
57 #endif
58 {
59         int res, need;
60         int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
61         va_list aq;
62
63         do {
64                 if (max_len < 0) {
65                         max_len = (*buf)->__AST_STR_LEN;        /* don't exceed the allocated space */
66                 }
67                 /*
68                  * Ask vsnprintf how much space we need. Remember that vsnprintf
69                  * does not count the final <code>'\0'</code> so we must add 1.
70                  */
71                 va_copy(aq, ap);
72                 res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
73
74                 need = res + offset + 1;
75                 /*
76                  * If there is not enough space and we are below the max length,
77                  * reallocate the buffer and return a message telling to retry.
78                  */
79                 if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
80                         if (max_len && max_len < need) {        /* truncate as needed */
81                                 need = max_len;
82                         } else if (max_len == 0) {      /* if unbounded, give more room for next time */
83                                 need += 16 + need / 4;
84                         }
85                         if (0) {        /* debugging */
86                                 ast_verbose("extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
87                         }
88                         if (
89 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
90                                         _ast_str_make_space(buf, need, file, lineno, function)
91 #else
92                                         ast_str_make_space(buf, need)
93 #endif
94                                 ) {
95                                 ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
96                                 va_end(aq);
97                                 return AST_DYNSTR_BUILD_FAILED;
98                         }
99                         (*buf)->__AST_STR_STR[offset] = '\0';   /* Truncate the partial write. */
100
101                         /* Restart va_copy before calling vsnprintf() again. */
102                         va_end(aq);
103                         continue;
104                 }
105                 va_end(aq);
106                 break;
107         } while (1);
108         /* update space used, keep in mind the truncation */
109         (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN : res + offset;
110
111         return res;
112 }
113
114 char *__ast_str_helper2(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
115 {
116         int dynamic = 0;
117         char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
118
119         if (maxlen < 1) {
120                 if (maxlen == 0) {
121                         dynamic = 1;
122                 }
123                 maxlen = (*buf)->__AST_STR_LEN;
124         }
125
126         while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
127                 if (escapecommas && (*src == '\\' || *src == ',')) {
128                         *ptr++ = '\\';
129                         maxlen--;
130                         (*buf)->__AST_STR_USED++;
131                 }
132                 *ptr++ = *src++;
133                 maxsrc--;
134                 maxlen--;
135                 (*buf)->__AST_STR_USED++;
136
137                 if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
138                         (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
139                         char *oldbase = (*buf)->__AST_STR_STR;
140                         size_t old = (*buf)->__AST_STR_LEN;
141                         if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
142                                 /* If the buffer can't be extended, end it. */
143                                 break;
144                         }
145                         /* What we extended the buffer by */
146                         maxlen = old;
147
148                         ptr += (*buf)->__AST_STR_STR - oldbase;
149                 }
150         }
151         if (__builtin_expect(!maxlen, 0)) {
152                 ptr--;
153         }
154         *ptr = '\0';
155         return (*buf)->__AST_STR_STR;
156 }
157