<p>George Joseph <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15505">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  George Joseph: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_odbc:  Introduce minargs config and expose ARGC in addition to ARGn.<br><br>minargs enables enforcing of minimum count of arguments to pass to<br>func_odbc, so if you're unconditionally using ARG1 through ARG4 then<br>this should be set to 4.  func_odbc will generate an error in this case,<br>so for example<br><br>[FOO]<br>minargs = 4<br><br>and ODBC_FOO(a,b,c) in dialplan will now error out instead of using a<br>potentially leaked ARG4 from Gosub().<br><br>ARGC is needed if you're using optional argument, to verify whether or<br>not an argument has been passed, else it's possible to use a leaked ARGn<br>from Gosub (app_stack).  So now you can safely do<br>${IF($[${ARGC}>3]?${ARGV}:default value)} kind of thing.<br><br>Change-Id: I6ca0b137d90b03f6aa9c496991f6cbf1518f6c24<br>Signed-off-by: Jaco Kroon <jaco@uls.co.za><br>---<br>M configs/samples/func_odbc.conf.sample<br>A doc/CHANGES-staging/func_odbc_ARGC_minargs.txt<br>M funcs/func_odbc.c<br>3 files changed, 60 insertions(+), 2 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/func_odbc.conf.sample b/configs/samples/func_odbc.conf.sample</span><br><span>index c467f7e..b825974 100644</span><br><span>--- a/configs/samples/func_odbc.conf.sample</span><br><span>+++ b/configs/samples/func_odbc.conf.sample</span><br><span>@@ -23,6 +23,10 @@</span><br><span> ; For substitution, you have ${ARG1}, ${ARG2} ... ${ARGn}</span><br><span> ; for the arguments to each SQL statement.</span><br><span> ;</span><br><span style="color: hsl(120, 100%, 40%);">+; Additionally you can use ${ARGC} to determine the number of arguments that</span><br><span style="color: hsl(120, 100%, 40%);">+; was actually passed (or risk using leaked ARGn variables from the channel).</span><br><span style="color: hsl(120, 100%, 40%);">+; Also reference the minargs configuration option.</span><br><span style="color: hsl(120, 100%, 40%);">+;</span><br><span> ; In addition, for write statements, you have ${VAL1}, ${VAL2} ... ${VALn}</span><br><span> ; parsed, just like arguments, for the values.  In addition, if you want the</span><br><span> ; whole value, never mind the parsing, you can get that with ${VALUE}.</span><br><span>@@ -87,6 +91,13 @@</span><br><span> ;              These additional rows can be returned by using the name of the</span><br><span> ;              function which was called to retrieve the first row as an</span><br><span> ;              argument to ODBC_FETCH().</span><br><span style="color: hsl(120, 100%, 40%);">+; minargs      The minimum number of ARGUMENTS that has to be passed to the</span><br><span style="color: hsl(120, 100%, 40%);">+;              function.  If fewer arguments than this is passed, then the call</span><br><span style="color: hsl(120, 100%, 40%);">+;              will fail.  It is important to note that unlike Gosub() and friends,</span><br><span style="color: hsl(120, 100%, 40%);">+;              func_odbc will not mask out ARGn variables that it's not actively</span><br><span style="color: hsl(120, 100%, 40%);">+;              using, as such, without this, it's entirely possible to use say</span><br><span style="color: hsl(120, 100%, 40%);">+;              ARG2 from the Gosub() inside func_odbc when the intent was to</span><br><span style="color: hsl(120, 100%, 40%);">+;              use an argument passed to func_odbc, but it simply was never passed.</span><br><span> </span><br><span> </span><br><span> ; ODBC_SQL - Allow an SQL statement to be built entirely in the dialplan</span><br><span>diff --git a/doc/CHANGES-staging/func_odbc_ARGC_minargs.txt b/doc/CHANGES-staging/func_odbc_ARGC_minargs.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..0984b50</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/func_odbc_ARGC_minargs.txt</span><br><span>@@ -0,0 +1,20 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: func_odbc</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Introduce an ARGC variable for func_odbc functions, along with a minargs</span><br><span style="color: hsl(120, 100%, 40%);">+per-function configuration option.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+minargs enables enforcing of minimum count of arguments to pass to</span><br><span style="color: hsl(120, 100%, 40%);">+func_odbc, so if you're unconditionally using ARG1 through ARG4 then</span><br><span style="color: hsl(120, 100%, 40%);">+this should be set to 4.  func_odbc will generate an error in this case,</span><br><span style="color: hsl(120, 100%, 40%);">+so for example</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+[FOO]</span><br><span style="color: hsl(120, 100%, 40%);">+minargs = 4</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+and ODBC_FOO(a,b,c) in dialplan will now error out instead of using a</span><br><span style="color: hsl(120, 100%, 40%);">+potentially leaked ARG4 from Gosub().</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ARGC is needed if you're using optional argument, to verify whether or</span><br><span style="color: hsl(120, 100%, 40%);">+not an argument has been passed, else it's possible to use a leaked ARGn</span><br><span style="color: hsl(120, 100%, 40%);">+from Gosub (app_stack).  So now you can safely do</span><br><span style="color: hsl(120, 100%, 40%);">+${IF($[${ARGC}>3]?${ARGV}:default value)} kind of thing.</span><br><span>diff --git a/funcs/func_odbc.c b/funcs/func_odbc.c</span><br><span>index 5cc0faa..9d6d0fc 100644</span><br><span>--- a/funcs/func_odbc.c</span><br><span>+++ b/funcs/func_odbc.c</span><br><span>@@ -120,6 +120,7 @@</span><br><span>         char *sql_insert;</span><br><span>    unsigned int flags;</span><br><span>  int rowlimit;</span><br><span style="color: hsl(120, 100%, 40%);">+ int minargs;</span><br><span>         struct ast_custom_function *acf;</span><br><span> };</span><br><span> </span><br><span>@@ -545,6 +546,14 @@</span><br><span>            return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STANDARD_APP_ARGS(args, s);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (args.argc < query->minargs) {</span><br><span style="color: hsl(120, 100%, 40%);">+               ast_log(LOG_ERROR, "%d arguments supplied to '%s' requiring minimum %d\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                          args.argc, cmd, query->minargs);</span><br><span style="color: hsl(120, 100%, 40%);">+           AST_RWLIST_UNLOCK(&queries);</span><br><span style="color: hsl(120, 100%, 40%);">+              return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  if (!chan) {</span><br><span>                 if (!(chan = ast_dummy_channel_alloc())) {</span><br><span>                   AST_RWLIST_UNLOCK(&queries);</span><br><span>@@ -578,7 +587,8 @@</span><br><span>               return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   AST_STANDARD_APP_ARGS(args, s);</span><br><span style="color: hsl(120, 100%, 40%);">+       snprintf(varname, sizeof(varname), "%u", args.argc);</span><br><span style="color: hsl(120, 100%, 40%);">+        pbx_builtin_pushvar_helper(chan, "ARGC", varname);</span><br><span>         for (i = 0; i < args.argc; i++) {</span><br><span>                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);</span><br><span>                pbx_builtin_pushvar_helper(chan, varname, args.field[i]);</span><br><span>@@ -603,6 +613,8 @@</span><br><span>              chan = ast_channel_unref(chan);</span><br><span>      } else {</span><br><span>             /* Restore prior values */</span><br><span style="color: hsl(120, 100%, 40%);">+            pbx_builtin_setvar_helper(chan, "ARGC", NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>           for (i = 0; i < args.argc; i++) {</span><br><span>                         snprintf(varname, sizeof(varname), "ARG%d", i + 1);</span><br><span>                        pbx_builtin_setvar_helper(chan, varname, NULL);</span><br><span>@@ -756,6 +768,14 @@</span><br><span>               return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STANDARD_APP_ARGS(args, s);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (args.argc < query->minargs) {</span><br><span style="color: hsl(120, 100%, 40%);">+               ast_log(LOG_ERROR, "%d arguments supplied to '%s' requiring minimum %d\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                          args.argc, cmd, query->minargs);</span><br><span style="color: hsl(120, 100%, 40%);">+           AST_RWLIST_UNLOCK(&queries);</span><br><span style="color: hsl(120, 100%, 40%);">+              return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  if (!chan) {</span><br><span>                 if (!(chan = ast_dummy_channel_alloc())) {</span><br><span>                   AST_RWLIST_UNLOCK(&queries);</span><br><span>@@ -768,7 +788,8 @@</span><br><span>               ast_autoservice_start(chan);</span><br><span>         }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   AST_STANDARD_APP_ARGS(args, s);</span><br><span style="color: hsl(120, 100%, 40%);">+       snprintf(varname, sizeof(varname), "%u", args.argc);</span><br><span style="color: hsl(120, 100%, 40%);">+        pbx_builtin_pushvar_helper(chan, "ARGC", varname);</span><br><span>         for (x = 0; x < args.argc; x++) {</span><br><span>                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);</span><br><span>                pbx_builtin_pushvar_helper(chan, varname, args.field[x]);</span><br><span>@@ -780,6 +801,8 @@</span><br><span>              chan = ast_channel_unref(chan);</span><br><span>      } else {</span><br><span>             /* Restore prior values */</span><br><span style="color: hsl(120, 100%, 40%);">+            pbx_builtin_setvar_helper(chan, "ARGC", NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>           for (x = 0; x < args.argc; x++) {</span><br><span>                         snprintf(varname, sizeof(varname), "ARG%d", x + 1);</span><br><span>                        pbx_builtin_setvar_helper(chan, varname, NULL);</span><br><span>@@ -1290,6 +1313,10 @@</span><br><span>                     sscanf(tmp, "%30d", &((*query)->rowlimit));</span><br><span>         }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if ((tmp = ast_variable_retrieve(cfg, catg, "minargs"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+          sscanf(tmp, "%30d", &((*query)->minargs));</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));</span><br><span>        if (!(*query)->acf) {</span><br><span>             free_acf_query(*query);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15505">change 15505</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/+/15505"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I6ca0b137d90b03f6aa9c496991f6cbf1518f6c24 </div>
<div style="display:none"> Gerrit-Change-Number: 15505 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Jaco Kroon <jaco@uls.co.za> </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>