From: Luigi Rizzo Date: Sun, 17 Dec 2006 22:57:46 +0000 (+0000) Subject: replace ast_build_string() with ast_str_*(). X-Git-Tag: 1.6.0-beta1~3^2~3704 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c5f06ddcdf3279793d9baf9ec0d925fc5df3e7e;p=thirdparty%2Fasterisk.git replace ast_build_string() with ast_str_*(). Unless i am very mistaken, function_realtime_read() was broken in that it would always return an empty string (because ast_build_string() advanced the pointer to the end of the string, and there was no reference to the initial value. This commit should fix this problem. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@48551 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/funcs/func_realtime.c b/funcs/func_realtime.c index 9610c0c2a3..4314c4e681 100644 --- a/funcs/func_realtime.c +++ b/funcs/func_realtime.c @@ -49,8 +49,9 @@ static int function_realtime_read(struct ast_channel *chan, char *cmd, char *dat { struct ast_variable *var, *head; struct ast_module_user *u; - char *results; - size_t resultslen = 0; + struct ast_str *out; + size_t resultslen; + int n; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(family); AST_APP_ARG(fieldmatch); @@ -80,13 +81,17 @@ static int function_realtime_read(struct ast_channel *chan, char *cmd, char *dat ast_module_user_remove(u); return -1; } + resultslen = 0; + n = 0; + for (var = head; var; n++, var = var->next) + resultslen += strlen(var->name) + strlen(var->value); + /* add space for delimiters and final '\0' */ + resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1; + + out = ast_str_alloca(resultslen); for (var = head; var; var = var->next) - resultslen += strlen(var->name) + strlen(var->value) + 2; - - results = alloca(resultslen); - for (var = head; var; var = var->next) - ast_build_string(&results, &resultslen, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1); - ast_copy_string(buf, results, len); + ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1); + ast_copy_string(buf, out->str, len); ast_module_user_remove(u);