<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18865">View Change</a></p><div style="white-space:pre-wrap">Approvals:
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pbx_functions.c: Manually update ast_str strlen.<br><br>When ast_func_read2 is used to read a function using<br>its read function (as opposed to a native ast_str read2<br>function), the result is copied directly by the function<br>into the ast_str buffer. As a result, the ast_str length<br>remains initialized to 0, which is a bug because this is<br>not the real string length.<br><br>This can cascade and have issues elsewhere, such as when<br>reading substrings of functions that only register read<br>as opposed to read2 callbacks. In this case, since reading<br>ast_str_strlen returns 0, the returned substring is empty<br>as opposed to the actual substring. This has caused<br>the ast_str family of functions to behave inconsistently<br>and erroneously, in contrast to the pbx_variables substitution<br>functions which work correctly.<br><br>This fixes this issue by manually updating the ast_str length<br>when the result is copied directly into the ast_str buffer.<br><br>Additionally, an assertion and a unit test that previously<br>exposed these issues are added, now that the issue is fixed.<br><br>ASTERISK-29966 #close<br><br>Change-Id: I4e2dba41410f9d4dff61c995d2ca27718248e07f<br>---<br>M main/pbx_functions.c<br>M main/pbx_variables.c<br>2 files changed, 74 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/pbx_functions.c b/main/pbx_functions.c</span><br><span>index 08cc191..081c33f 100644</span><br><span>--- a/main/pbx_functions.c</span><br><span>+++ b/main/pbx_functions.c</span><br><span>@@ -678,6 +678,7 @@</span><br><span> ast_str_make_space(str, maxsize);</span><br><span> }</span><br><span> res = acfptr->read(chan, copy, args, ast_str_buffer(*str), maxsize);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_update(*str); /* Manually set the string length */</span><br><span> }</span><br><span> if (acfptr->mod && u) {</span><br><span> __ast_module_user_remove(acfptr->mod, u);</span><br><span>diff --git a/main/pbx_variables.c b/main/pbx_variables.c</span><br><span>index 45b7ca0..f589b6b 100644</span><br><span>--- a/main/pbx_variables.c</span><br><span>+++ b/main/pbx_variables.c</span><br><span>@@ -40,6 +40,7 @@</span><br><span> #include "asterisk/paths.h"</span><br><span> #include "asterisk/pbx.h"</span><br><span> #include "asterisk/stasis_channels.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/test.h"</span><br><span> #include "pbx_private.h"</span><br><span> </span><br><span> /*** DOCUMENTATION</span><br><span>@@ -189,6 +190,8 @@</span><br><span> </span><br><span> lr = ast_str_strlen(value); /* compute length after copy, so we never go out of the workspace */</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ ast_assert(lr == strlen(ast_str_buffer(value))); /* ast_str_strlen should always agree with strlen */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Quick check if no need to do anything */</span><br><span> if (offset == 0 && length >= lr) /* take the whole string */</span><br><span> return ast_str_buffer(value);</span><br><span>@@ -1292,6 +1295,74 @@</span><br><span> ast_rwlock_unlock(&globalslock);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef TEST_FRAMEWORK</span><br><span style="color: hsl(120, 100%, 40%);">+AST_TEST_DEFINE(test_variable_substrings)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int i, res = AST_TEST_PASS;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_channel *chan; /* dummy channel */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str *str; /* fancy string for holding comparing value */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *test_strings[][5] = {</span><br><span style="color: hsl(120, 100%, 40%);">+ {"somevaluehere", "CALLERID(num):0:25", "somevaluehere"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"somevaluehere", "CALLERID(num):0:5", "somev"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"somevaluehere", "CALLERID(num):4:5", "value"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"somevaluehere", "CALLERID(num):0:-4", "somevalue"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"somevaluehere", "CALLERID(num):-4", "here"},</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ info->name = "variable_substrings";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->category = "/main/pbx/";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->summary = "Test variable substring resolution";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->description = "Verify that variable substrings are calculated correctly";</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_NOT_RUN;</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_EXECUTE:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(chan = ast_dummy_channel_alloc())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_status_update(test, "Unable to allocate dummy channel\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(str = ast_str_create(64))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_release(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < ARRAY_LEN(test_strings); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char substituted[512], tmp[512] = "";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_callerid(chan, test_strings[i][0], NULL, test_strings[i][0]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(tmp, sizeof(tmp), "${%s}", test_strings[i][1]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* test ast_str_substitute_variables */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_substitute_variables(&str, 0, chan, tmp);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Comparing STR %s with %s\n", ast_str_buffer(str), test_strings[i][2]);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp(test_strings[i][2], ast_str_buffer(str))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_status_update(test, "Format string '%s' substituted to '%s' using str sub. Expected '%s'.\n", test_strings[i][0], ast_str_buffer(str), test_strings[i][2]);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_TEST_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* test pbx_substitute_variables_helper */</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_substitute_variables_helper(chan, tmp, substituted, sizeof(substituted) - 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Comparing PBX %s with %s\n", substituted, test_strings[i][2]);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp(test_strings[i][2], substituted)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_status_update(test, "Format string '%s' substituted to '%s' using pbx sub. Expected '%s'.\n", test_strings[i][0], substituted, test_strings[i][2]);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_TEST_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_release(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static struct ast_cli_entry vars_cli[] = {</span><br><span> AST_CLI_DEFINE(handle_show_globals, "Show global dialplan variables"),</span><br><span> AST_CLI_DEFINE(handle_show_chanvar, "Show channel variables"),</span><br><span>@@ -1306,6 +1377,7 @@</span><br><span> ast_unregister_application("Set");</span><br><span> ast_unregister_application("MSet");</span><br><span> pbx_builtin_clear_globals();</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_UNREGISTER(test_variable_substrings);</span><br><span> }</span><br><span> </span><br><span> int load_pbx_variables(void)</span><br><span>@@ -1316,6 +1388,7 @@</span><br><span> res |= ast_register_application2("Set", pbx_builtin_setvar, NULL, NULL, NULL);</span><br><span> res |= ast_register_application2("MSet", pbx_builtin_setvar_multiple, NULL, NULL, NULL);</span><br><span> ast_register_cleanup(unload_pbx_variables);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_REGISTER(test_variable_substrings);</span><br><span> </span><br><span> return res;</span><br><span> }</span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18865">change 18865</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18865"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: I4e2dba41410f9d4dff61c995d2ca27718248e07f </div>
<div style="display:none"> Gerrit-Change-Number: 18865 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>