<p>Corey Farrell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/7661">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">CLI: Address multiple issues.<br><br>* listen uses the variable `s` for the result from ast_poll() then<br>  overwrites it with the result of accept().  Create a separate variable<br>  poll_result to avoid confusion since ast_poll does not return a file<br>  descriptor.<br>* Reserve an extra byte while processing completion results from remote<br>  daemon.  This fixes a bug where completion processing used strstr() on<br>  a string that was not '\0' terminated.  This was no risk to the Asterisk<br>  daemon, the bug was only reachable the remote console process.<br>* Resolve leak in handle_showchan when the channel is not found.<br>* Multiple leaks in pbx_config CLI completion.<br>* Fix leaks in "manager show command".<br><br>Change-Id: I8f633ceb1714867ae30ef4e421858f77c14485a9<br>---<br>M main/asterisk.c<br>M main/cli.c<br>M main/manager.c<br>M pbx/pbx_config.c<br>4 files changed, 33 insertions(+), 11 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/7661/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/main/asterisk.c b/main/asterisk.c<br>index 75d611f..6b427fb 100644<br>--- a/main/asterisk.c<br>+++ b/main/asterisk.c<br>@@ -1573,17 +1573,21 @@<br>      int s;<br>        socklen_t len;<br>        int x;<br>+       int poll_result;<br>      struct pollfd fds[1];<br>+<br>      for (;;) {<br>-           if (ast_socket < 0)<br>+               if (ast_socket < 0) {<br>                      return NULL;<br>+         }<br>             fds[0].fd = ast_socket;<br>               fds[0].events = POLLIN;<br>-              s = ast_poll(fds, 1, -1);<br>+            poll_result = ast_poll(fds, 1, -1);<br>           pthread_testcancel();<br>-                if (s < 0) {<br>-                      if (errno != EINTR)<br>+          if (poll_result < 0) {<br>+                    if (errno != EINTR) {<br>                                 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));<br>+                        }<br>                     continue;<br>             }<br>             len = sizeof(sunaddr);<br>@@ -3051,9 +3055,10 @@<br> #define CMD_MATCHESARRAY "_COMMAND MATCHESARRAY \"%s\" \"%s\""<br>                 char *mbuf;<br>           char *new_mbuf;<br>-              int mlen = 0, maxmbuf = 2048;<br>+                int mlen = 0;<br>+                int maxmbuf = 2049;<br> <br>-               /* Start with a 2048 byte buffer */<br>+          /* Start with a 2049 byte buffer */<br>           mbuf = ast_malloc(maxmbuf);<br> <br>                /* This will run snprintf twice at most. */<br>@@ -3074,12 +3079,14 @@<br>          res = 0;<br>              mlen = 0;<br>             mbuf[0] = '\0';<br>+              /* Reserve space for '\0' */<br>+         maxmbuf--;<br> <br>                 while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {<br>                    if (mlen + 1024 > maxmbuf) {<br>-                              /* Expand buffer to the next 1024 byte increment. */<br>+                         /* Expand buffer to the next 1024 byte increment plus a NULL terminator. */<br>                           maxmbuf = mlen + 1024;<br>-                               new_mbuf = ast_realloc(mbuf, maxmbuf);<br>+                               new_mbuf = ast_realloc(mbuf, maxmbuf + 1);<br>                            if (!new_mbuf) {<br>                                      ast_free(mbuf);<br>                                       *((char *) lf->cursor) = savechr;<br>@@ -3092,6 +3099,7 @@<br>                   res = read(ast_consock, mbuf + mlen, 1024);<br>                   if (res > 0) {<br>                             mlen += res;<br>+                         mbuf[mlen] = '\0';<br>                    }<br>             }<br>             mbuf[mlen] = '\0';<br>diff --git a/main/cli.c b/main/cli.c<br>index 207d1f8..3d60880 100644<br>--- a/main/cli.c<br>+++ b/main/cli.c<br>@@ -1504,6 +1504,8 @@<br>    chan = ast_channel_get_by_name(a->argv[3]);<br>        if (!chan) {<br>          ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);<br>+           ast_free(output);<br>+<br>          return CLI_SUCCESS;<br>   }<br> <br>diff --git a/main/manager.c b/main/manager.c<br>index 079dab7..08da656 100644<br>--- a/main/manager.c<br>+++ b/main/manager.c<br>@@ -2343,10 +2343,11 @@<br>                AST_RWLIST_UNLOCK(&actions);<br>              return ret;<br>   }<br>-    authority = ast_str_alloca(MAX_AUTH_PERM_STRING);<br>     if (a->argc < 4) {<br>              return CLI_SHOWUSAGE;<br>         }<br>+<br>+ authority = ast_str_alloca(MAX_AUTH_PERM_STRING);<br> <br> #ifdef AST_XML_DOCS<br>    /* setup the titles */<br>@@ -2375,6 +2376,7 @@<br>                                         char *seealso = ast_xmldoc_printable(S_OR(cur->seealso, "Not available"), 1);<br>                                    char *privilege = ast_xmldoc_printable(S_OR(auth_str, "Not available"), 1);<br>                                         char *responses = ast_xmldoc_printable("None", 1);<br>+<br>                                       ast_cli(a->fd, "%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s",<br>                                             syntax_title, syntax,<br>                                                 synopsis_title, synopsis,<br>@@ -2402,6 +2404,14 @@<br>                                             ast_cli(a->fd, "Event: %s\n", cur->final_response->name);<br>                                          print_event_instance(a, cur->final_response);<br>                                      }<br>+<br>+                                 ast_free(syntax);<br>+                                    ast_free(synopsis);<br>+                                  ast_free(description);<br>+                                       ast_free(arguments);<br>+                                 ast_free(seealso);<br>+                                   ast_free(privilege);<br>+                                 ast_free(responses);<br>                          } else<br> #endif<br>                               {<br>diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c<br>index b96914d..f292ed4 100644<br>--- a/pbx/pbx_config.c<br>+++ b/pbx/pbx_config.c<br>@@ -322,8 +322,10 @@<br>                              while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)<br>                                   already_served = lookup_ci(nc, i_name);<br> <br>-                           if (!already_served && ++which > a->n)<br>+                         if (!already_served && ++which > a->n) {<br>                                        res = ast_strdup(i_name);<br>+                                    break;<br>+                               }<br>                     }<br>                     ast_unlock_context(c);<br>                }<br>@@ -1559,7 +1561,7 @@<br>              }<br>             ast_unlock_contexts();<br>                ast_free(dupline);<br>-           return NULL;<br>+         return ret;<br>   }<br> <br>  return NULL;<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/7661">change 7661</a>. To unsubscribe, 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/7661"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I8f633ceb1714867ae30ef4e421858f77c14485a9 </div>
<div style="display:none"> Gerrit-Change-Number: 7661 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>