presence: wrap error macro in do {} while(0), safety checks
authorDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 13 Jul 2017 14:50:05 +0000 (16:50 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 13 Jul 2017 14:50:05 +0000 (16:50 +0200)
- free ws_list pointer when sphere is enabled
- copy macros not ended with ; in definition -- done it when using

src/modules/presence/event_list.c
src/modules/presence/hash.c
src/modules/presence/hash.h
src/modules/presence/presence.c
src/modules/presence/presentity.c
src/modules/presence/subscribe.c

index 5f19ad4..1bce392 100644 (file)
@@ -389,12 +389,14 @@ int search_event_params(event_t* ev, event_t* searched_ev)
 
                while(p)
                {
-                       if(p->name.len== ps->name.len &&
-                               strncmp(p->name.s,ps->name.s, ps->name.len)== 0) {
-                               if((p->body.s== 0 && ps->body.s== 0) ||
-                                       (p->body.len== ps->body.len &&
-                                       strncmp(p->body.s,ps->body.s,ps->body.len)== 0))
-                               {
+                       if(p->name.len==ps->name.len &&
+                                       strncmp(p->name.s, ps->name.s, ps->name.len)==0) {
+                               if(p->body.s==0 && ps->body.s==0) {
+                                       found= 1;
+                                       break;
+                               }
+                               if(p->body.s!=0 && ps->body.s!=0 && p->body.len==ps->body.len
+                                               && (strncmp(p->body.s, ps->body.s, ps->body.len)==0)) {
                                        found= 1;
                                        break;
                                }
index f4a86c0..ee2321e 100644 (file)
@@ -154,25 +154,25 @@ subs_t* mem_copy_subs(subs_t* s, int mem_type)
        memset(dest, 0, size);
        size= sizeof(subs_t);
 
-       CONT_COPY(dest, dest->pres_uri, s->pres_uri)
-       CONT_COPY(dest, dest->to_user, s->to_user)
-       CONT_COPY(dest, dest->to_domain, s->to_domain)
-       CONT_COPY(dest, dest->from_user, s->from_user)
-       CONT_COPY(dest, dest->from_domain, s->from_domain)
-       CONT_COPY(dest, dest->watcher_user, s->watcher_user)
-       CONT_COPY(dest, dest->watcher_domain, s->watcher_domain)
-       CONT_COPY(dest, dest->to_tag, s->to_tag)
-       CONT_COPY(dest, dest->from_tag, s->from_tag)
-       CONT_COPY(dest, dest->callid, s->callid)
-       CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str)
-       CONT_COPY(dest, dest->local_contact, s->local_contact)
-       CONT_COPY(dest, dest->contact, s->contact)
-       CONT_COPY(dest, dest->record_route, s->record_route)
-       CONT_COPY(dest, dest->user_agent, s->user_agent)
+       CONT_COPY(dest, dest->pres_uri, s->pres_uri);
+       CONT_COPY(dest, dest->to_user, s->to_user);
+       CONT_COPY(dest, dest->to_domain, s->to_domain);
+       CONT_COPY(dest, dest->from_user, s->from_user);
+       CONT_COPY(dest, dest->from_domain, s->from_domain);
+       CONT_COPY(dest, dest->watcher_user, s->watcher_user);
+       CONT_COPY(dest, dest->watcher_domain, s->watcher_domain);
+       CONT_COPY(dest, dest->to_tag, s->to_tag);
+       CONT_COPY(dest, dest->from_tag, s->from_tag);
+       CONT_COPY(dest, dest->callid, s->callid);
+       CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str);
+       CONT_COPY(dest, dest->local_contact, s->local_contact);
+       CONT_COPY(dest, dest->contact, s->contact);
+       CONT_COPY(dest, dest->record_route, s->record_route);
+       CONT_COPY(dest, dest->user_agent, s->user_agent);
        if(s->event_id.s)
-               CONT_COPY(dest, dest->event_id, s->event_id)
+               CONT_COPY(dest, dest->event_id, s->event_id);
        if(s->reason.s)
-               CONT_COPY(dest, dest->reason, s->reason)
+               CONT_COPY(dest, dest->reason, s->reason);
 
        dest->event= s->event;
        dest->local_cseq= s->local_cseq;
@@ -187,13 +187,6 @@ subs_t* mem_copy_subs(subs_t* s, int mem_type)
        return dest;
 
 error:
-       if(dest)
-       {
-               if(mem_type & PKG_MEM_TYPE)
-                       pkg_free(dest);
-               else
-                       shm_free(dest);
-       }
        return NULL;
 }
 
@@ -219,24 +212,24 @@ subs_t* mem_copy_subs_noc(subs_t* s)
        memset(dest, 0, size);
        size= sizeof(subs_t);
 
-       CONT_COPY(dest, dest->pres_uri, s->pres_uri)
-       CONT_COPY(dest, dest->to_user, s->to_user)
-       CONT_COPY(dest, dest->to_domain, s->to_domain)
-       CONT_COPY(dest, dest->from_user, s->from_user)
-       CONT_COPY(dest, dest->from_domain, s->from_domain)
-       CONT_COPY(dest, dest->watcher_user, s->watcher_user)
-       CONT_COPY(dest, dest->watcher_domain, s->watcher_domain)
-       CONT_COPY(dest, dest->to_tag, s->to_tag)
-       CONT_COPY(dest, dest->from_tag, s->from_tag)
-       CONT_COPY(dest, dest->callid, s->callid)
-       CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str)
-       CONT_COPY(dest, dest->local_contact, s->local_contact)
-       CONT_COPY(dest, dest->record_route, s->record_route)
-       CONT_COPY(dest, dest->user_agent, s->user_agent)
+       CONT_COPY(dest, dest->pres_uri, s->pres_uri);
+       CONT_COPY(dest, dest->to_user, s->to_user);
+       CONT_COPY(dest, dest->to_domain, s->to_domain);
+       CONT_COPY(dest, dest->from_user, s->from_user);
+       CONT_COPY(dest, dest->from_domain, s->from_domain);
+       CONT_COPY(dest, dest->watcher_user, s->watcher_user);
+       CONT_COPY(dest, dest->watcher_domain, s->watcher_domain);
+       CONT_COPY(dest, dest->to_tag, s->to_tag);
+       CONT_COPY(dest, dest->from_tag, s->from_tag);
+       CONT_COPY(dest, dest->callid, s->callid);
+       CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str);
+       CONT_COPY(dest, dest->local_contact, s->local_contact);
+       CONT_COPY(dest, dest->record_route, s->record_route);
+       CONT_COPY(dest, dest->user_agent, s->user_agent);
        if(s->event_id.s)
-               CONT_COPY(dest, dest->event_id, s->event_id)
+               CONT_COPY(dest, dest->event_id, s->event_id);
        if(s->reason.s)
-               CONT_COPY(dest, dest->reason, s->reason)
+               CONT_COPY(dest, dest->reason, s->reason);
 
        dest->event= s->event;
        dest->local_cseq= s->local_cseq;
index 28c8cbf..59731c2 100644 (file)
@@ -41,16 +41,18 @@ struct presentity;
 #define PKG_MEM_STR       "pkg"
 #define SHARE_MEM         "share"
 
-#define ERR_MEM(mem_type)  LM_ERR("No more %s memory\n",mem_type);\
-                                               goto error
+#define ERR_MEM(mem_type)  do { \
+                               LM_ERR("No more %s memory\n",mem_type); \
+                               goto error; \
+                       } while(0)
 
 #define CONT_COPY(buf, dest, source)\
        do{ \
-       dest.s= (char*)buf+ size;\
-       memcpy(dest.s, source.s, source.len);\
-       dest.len= source.len;\
-       size+= source.len; \
-       } while(0);
+               dest.s= (char*)buf+ size; \
+               memcpy(dest.s, source.s, source.len); \
+               dest.len= source.len; \
+               size+= source.len; \
+       } while(0)
 
 #define PKG_MEM_TYPE     1<< 1
 #define SHM_MEM_TYPE     1<< 2
index 9bd1f22..ad4275f 100644 (file)
@@ -828,13 +828,13 @@ int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc)
        int err_ret= -1;
        int n= 0;
 
-       typedef struct ws
-       {
+       typedef struct ws {
                int status;
                str reason;
                str w_user;
                str w_domain;
-       }ws_t;
+       } ws_t;
+
        ws_t* ws_list= NULL;
 
        LM_DBG("start\n");
@@ -897,7 +897,7 @@ int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc)
        hash_code= core_case_hash(&pres_uri, &ev->name, shtable_size);
        subs.db_flag= hash_code;
 
-       /*must do a copy as sphere_check requires database queries */
+       /* must do a copy as sphere_check requires database queries */
        if(sphere_enable)
        {
                n= result->n;
@@ -987,6 +987,7 @@ int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc)
                        if(ws_list[i].reason.s)
                                pkg_free(ws_list[i].reason.s);
                }
+               pkg_free(ws_list);
                ws_list= NULL;
 
                goto send_notify;
@@ -1066,13 +1067,12 @@ done:
                {
                        if(ws_list[i].w_user.s)
                                pkg_free(ws_list[i].w_user.s);
-                       else
-                               break;
                        if(ws_list[i].w_domain.s)
                                pkg_free(ws_list[i].w_domain.s);
                        if(ws_list[i].reason.s)
                                pkg_free(ws_list[i].reason.s);
                }
+               pkg_free(ws_list);
        }
        return err_ret;
 }
index e3246e2..eb029a4 100644 (file)
@@ -425,7 +425,9 @@ int delete_presentity_if_dialog_id_exists(presentity_t* presentity, char* dialog
 
                                LM_WARN("Presentity already exists - deleting it\n");
 
-                               delete_presentity(&old_presentity);
+                               if(delete_presentity(&old_presentity)<0) {
+                                       LM_ERR("failed to delete presentity\n");
+                               }
 
                                pa_dbf.free_result(pa_db, result);
                                result = NULL;
@@ -513,6 +515,7 @@ int get_dialog_state(presentity_t* presentity, char** state)
                return 0;
        }
 
+       parse_state_result = 0;
        // Loop the rows returned from the DB
        for (i=0; i < result->n; i++)
        {
@@ -524,15 +527,17 @@ int get_dialog_state(presentity_t* presentity, char** state)
                parse_state_result = parse_dialog_state_from_body(tmp_db_body,
                                &db_is_dialog, state);
 
-               pa_dbf.free_result(pa_db, result);
-               result = NULL;
-
-               return parse_state_result;
+               if(parse_state_result==0) {
+                       /* successful parsing */
+                       pa_dbf.free_result(pa_db, result);
+                       result = NULL;
+                       return parse_state_result;
+               }
        }
 
        pa_dbf.free_result(pa_db, result);
        result = NULL;
-       return 0;
+       return parse_state_result;
 }
 
 int is_dialog_terminated(presentity_t* presentity)
index 446b657..ee90bf7 100644 (file)
@@ -1003,7 +1003,7 @@ int handle_subscribe0(struct sip_msg* msg)
 {
        struct to_body *pfrom;
 
-       if (parse_from_uri(msg) < 0)
+       if (parse_from_uri(msg) == NULL)
        {
                LM_ERR("failed to find From header\n");
                if (slb.freply(msg, 400, &pu_400_rpl) < 0)