[Home]

Summary:ASTERISK-28459: pbx: Asterisk is dropping part of a string passed to functions
Reporter:Jan Blom (jblom)Labels:patch
Date Opened:2019-06-24 05:06:14Date Closed:
Priority:MinorRegression?
Status:Open/NewComponents:Core/PBX Functions/General
Versions:16.4.0 Frequency of
Occurrence
Constant
Related
Issues:
Environment:CentOS Linux release 7.6 with Asterisk 16.4.0Attachments:( 0) pbx_variables.patch
Description:We need to pass around variables containing special characters, i.e. non-alfanumeric chars. This typically include SIP Call-Id from some vendors.

We have so far managed to deal with all situations using quotes and escape character in accordance with https://wiki.asterisk.org/wiki/display/AST/Parameter+Quoting.

However when having a string containing a closing parenthesis _and_ a colon, the string gets cut-off when passing it to a function. This becomes a big issue for us since this affects ODBC database calls as well as for example SQL_ESC() function.

Calling this dialplan results in the output below:
{code}
[test]
exten => main,1,NoOp
same => n,Set(LOCAL(input)=1234ab)cd:efg)

same => n,Verbose(0,--- Test 1 ---)
same => n,Verbose(0,Input:    ${input})
same => n,Gosub(sub,1(${input}))
same => n,Set(LOCAL(output)=${GOSUB_RETVAL})
same => n,Verbose(0,Output:   ${output})
same => n,Verbose(0,Function: ${PASSTHRU(${input})})

same => n,Verbose(0,--- Test 2 ---)
same => n,Verbose(0,Input:    ${input})
same => n,Gosub(sub,1("${input}"))
same => n,Set(LOCAL(output)=${GOSUB_RETVAL})
same => n,Verbose(0,Output:   ${output})
same => n,Verbose(0,Function: ${PASSTHRU("${input}")})

same => n,Verbose(0,--- Test done ---)
same => n,Return


exten => sub,1,NoOp
same => n,Verbose(0,In sub:   ${ARG1})
same => n,Return(${ARG1})
{code}

Result:
{noformat}
--- Test 1 ---
Input:    1234ab)cd:efg
In sub:   1234ab)cd:efg
Output:   1234ab)cd:efg
Function: 1234ab
--- Test 2 ---
Input:    1234ab)cd:efg
In sub:   "1234ab)cd:efg"
Output:   "1234ab)cd:efg"
Function: "1234ab
--- Test done ---
{noformat}

You can see the cut-off string returned from PASSTHRU(). We could use any function that takes a string as input and have the same result. The test shows that calling subroutines with Gosub works fine though.

It doesn't matter if we surround the string in quotes (the difference between Test 1 ans Test 2) or if we escape the parenthesis and/or colon in any combination with a backslash. We always get the corrupted string passed into the function.

We haven't been able to find any workaround to this problem. It breaks our database calling, using ODBC.
Comments:By: Asterisk Team (asteriskteam) 2019-06-24 05:06:16.134-0500

Thanks for creating a report! The issue has entered the triage process. That means the issue will wait in this status until a Bug Marshal has an opportunity to review the issue. Once the issue has been reviewed you will receive comments regarding the next steps towards resolution.

A good first step is for you to review the [Asterisk Issue Guidelines|https://wiki.asterisk.org/wiki/display/AST/Asterisk+Issue+Guidelines] if you haven't already. The guidelines detail what is expected from an Asterisk issue report.

Then, if you are submitting a patch, please review the [Patch Contribution Process|https://wiki.asterisk.org/wiki/display/AST/Patch+Contribution+Process].

Please note that once your issue enters an open state it has been accepted. As Asterisk is an open source project there is no guarantee or timeframe on when your issue will be looked into. If you need expedient resolution you will need to find and pay a suitable developer. Asking for an update on your issue will not yield any progress on it and will not result in a response. All updates are posted to the issue when they occur.

By: Joshua C. Colp (jcolp) 2019-06-24 05:40:15.466-0500

This is the result of the "parse_variable_name" function in main/pbx.c. It seems to be seeing this as usage of the offset functionality for variables.

By: Jan Blom (jblom) 2019-06-24 18:38:16.991-0500

First patch attempt

By: Jan Blom (jblom) 2019-06-24 18:49:30.811-0500

Thanks for the pointer! The function parse_variable_name() is located in main/pbx_variables.c.

I have made a very simple patch for the parser to honor '\' as escape character. This makes all my test cases to go through.

{code:title=The updated function in main/pbx_variables.c}
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
{
       int parens = 0;
       int skip_next = 0;

       *offset = 0;
       *length = INT_MAX;
       *isfunc = 0;
       for (; *var; var++) {
               if (skip_next != 0) {
                       /* skip this char */
                       skip_next = 0;
               } else if (*var == '\\') {
                       skip_next = 1;
               } else  if (*var == '(') {
                       (*isfunc)++;
                       parens++;
               } else if (*var == ')') {
                       parens--;
               } else if (*var == ':' && parens == 0) {
                       *var++ = '\0';
                       sscanf(var, "%30d:%30d", offset, length);
                       return 1; /* offset:length valid */
               }
       }
       return 0;
}
{code}

Patch file has been attached to this Jira.