]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
app_voicemail: Fix pollmailboxes
authorSean Bright <sean.bright@gmail.com>
Wed, 26 Aug 2020 13:55:37 +0000 (09:55 -0400)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Mon, 31 Aug 2020 13:50:16 +0000 (08:50 -0500)
The name of the voicemail context was overwriting the name of the
subscribed mailbox. Fix by simplifying how we create the MWI
subscription.

ASTERISK-29029 #close

Change-Id: Ie8a7db6a0b68f3995b0846bbb733a21909ba44e5

apps/app_voicemail.c

index 742c9c36eb41e64c41b3f0349176635eba8f20be..a19212255b225130e896818b726331d0a4058214 100644 (file)
@@ -1017,23 +1017,9 @@ struct mwi_sub {
        int old_new;
        int old_old;
        char *uniqueid;
-       char mailbox[0];
-};
-
-struct mwi_sub_task {
-       const char *mailbox;
-       const char *context;
-       const char *uniqueid;
+       char *mailbox;
 };
 
-static void mwi_sub_task_dtor(struct mwi_sub_task *mwist)
-{
-       ast_free((void *) mwist->mailbox);
-       ast_free((void *) mwist->context);
-       ast_free((void *) mwist->uniqueid);
-       ast_free(mwist);
-}
-
 static struct ast_taskprocessor *mwi_subscription_tps;
 
 static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
@@ -13286,6 +13272,7 @@ static void *mb_poll_thread(void *data)
 static void mwi_sub_destroy(struct mwi_sub *mwi_sub)
 {
        ast_free(mwi_sub->uniqueid);
+       ast_free(mwi_sub->mailbox);
        ast_free(mwi_sub);
 }
 
@@ -13364,35 +13351,12 @@ static int handle_unsubscribe(void *datap)
 
 static int handle_subscribe(void *datap)
 {
-       unsigned int len;
-       struct mwi_sub *mwi_sub;
-       struct mwi_sub_task *p = datap;
-       size_t context_len;
-
-       len = sizeof(*mwi_sub) + 1;
-       if (!ast_strlen_zero(p->mailbox))
-               len += strlen(p->mailbox);
-
-       context_len = strlen(p->context) + 1; /* Allow for seperator */
-       if (!ast_strlen_zero(p->context))
-               len += context_len;
-
-       if (!(mwi_sub = ast_calloc(1, len)))
-               return -1;
-
-       mwi_sub->uniqueid = ast_strdup(p->uniqueid);
-       if (!ast_strlen_zero(p->mailbox))
-               strcpy(mwi_sub->mailbox, p->mailbox);
-
-       if (!ast_strlen_zero(p->context)) {
-               strcat(mwi_sub->mailbox, "@");
-               ast_copy_string(mwi_sub->mailbox, p->context, context_len);
-       }
+       struct mwi_sub *mwi_sub = datap;
 
        AST_RWLIST_WRLOCK(&mwi_subs);
        AST_RWLIST_INSERT_TAIL(&mwi_subs, mwi_sub, entry);
        AST_RWLIST_UNLOCK(&mwi_subs);
-       mwi_sub_task_dtor(p);
+
        poll_subscribed_mailbox(mwi_sub);
        return 0;
 }
@@ -13413,29 +13377,39 @@ static void mwi_unsub_event_cb(struct stasis_subscription_change *change)
 
 static void mwi_sub_event_cb(struct stasis_subscription_change *change)
 {
-       struct mwi_sub_task *mwist;
+       struct mwi_sub *mwi_sub;
        const char *topic;
        char *context;
        char *mailbox;
 
-       mwist = ast_calloc(1, (sizeof(*mwist)));
-       if (!mwist) {
+       mwi_sub = ast_calloc(1, sizeof(*mwi_sub));
+       if (!mwi_sub) {
                return;
        }
 
        /* The topic name is prefixed with "mwi:all/" as this is a pool topic */
        topic = stasis_topic_name(change->topic) + 8;
        if (separate_mailbox(ast_strdupa(topic), &mailbox, &context)) {
-               ast_free(mwist);
+               mwi_sub_destroy(mwi_sub);
                return;
        }
 
-       mwist->mailbox = ast_strdup(mailbox);
-       mwist->context = ast_strdup(context);
-       mwist->uniqueid = ast_strdup(change->uniqueid);
+       /* separate_mailbox() guarantees a non-NULL, non-empty mailbox and context */
+       if (ast_asprintf(&mwi_sub->mailbox, "%s@%s", mailbox, context) < 0) {
+               mwi_sub_destroy(mwi_sub);
+               return;
+       }
+
+       /* The stasis subscription uniqueid will never be NULL */
+       mwi_sub->uniqueid = ast_strdup(change->uniqueid);
+       if (!mwi_sub->uniqueid) {
+               mwi_sub_destroy(mwi_sub);
+               return;
+       }
 
-       if (ast_taskprocessor_push(mwi_subscription_tps, handle_subscribe, mwist) < 0) {
-               mwi_sub_task_dtor(mwist);
+       if (ast_taskprocessor_push(mwi_subscription_tps, handle_subscribe, mwi_sub) < 0) {
+               mwi_sub_destroy(mwi_sub);
+               return;
        }
 }