Avoid crash in generic CC agent init if caller name or number is NULL.
[asterisk/asterisk.git] / main / ccss.c
index 583c738..04fa92a 100644 (file)
@@ -98,8 +98,17 @@ static unsigned int global_cc_max_requests;
  */
 static int cc_request_count;
 
-#define cc_ref(obj, debug) ({ao2_t_ref((obj), +1, (debug)); (obj);})
-#define cc_unref(obj, debug) ({ao2_t_ref((obj), -1, (debug)); NULL;})
+static inline void *cc_ref(void *obj, const char *debug)
+{
+       ao2_t_ref(obj, +1, debug);
+       return obj;
+}
+
+static inline void *cc_unref(void *obj, const char *debug)
+{
+       ao2_t_ref(obj, -1, debug);
+       return NULL;
+}
 
 /*!
  * \since 1.8
@@ -2330,8 +2339,12 @@ static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel
        }
 
        generic_pvt->offer_timer_id = -1;
-       ast_copy_string(generic_pvt->cid_num, chan->cid.cid_num, sizeof(generic_pvt->cid_num));
-       ast_copy_string(generic_pvt->cid_name, chan->cid.cid_name, sizeof(generic_pvt->cid_name));
+       if (chan->cid.cid_num) {
+               ast_copy_string(generic_pvt->cid_num, chan->cid.cid_num, sizeof(generic_pvt->cid_num));
+       }
+       if (chan->cid.cid_name) {
+               ast_copy_string(generic_pvt->cid_name, chan->cid.cid_name, sizeof(generic_pvt->cid_name));
+       }
        ast_copy_string(generic_pvt->exten, S_OR(chan->macroexten, chan->exten), sizeof(generic_pvt->exten));
        ast_copy_string(generic_pvt->context, S_OR(chan->macrocontext, chan->context), sizeof(generic_pvt->context));
        agent->private_data = generic_pvt;
@@ -2341,13 +2354,13 @@ static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel
 
 static int offer_timer_expire(const void *data)
 {
-       const struct ast_cc_agent *agent = data;
+       struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
        struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
        ast_log_dynamic_level(cc_logger_level, "Core %d: Queuing change request because offer timer has expired.\n",
                        agent->core_id);
        agent_pvt->offer_timer_id = -1;
        ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
-       cc_unref((struct ast_cc_agent *)agent, "Remove scheduler's reference to the agent");
+       cc_unref(agent, "Remove scheduler's reference to the agent");
        return 0;
 }