presence(k): avoid str* alloc for local contact
authorDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 3 Feb 2011 22:54:13 +0000 (23:54 +0100)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 3 Feb 2011 22:54:13 +0000 (23:54 +0100)
- it is not used further an leads to memleak

modules_k/presence/subscribe.c
modules_k/presence/utils_func.h

index e96efc0..d37518a 100644 (file)
@@ -921,7 +921,6 @@ int extract_sdialog_info(subs_t* subs,struct sip_msg* msg, int mexp,
 {
        str rec_route= {0, 0};
        int rt  = 0;
-       str* contact= NULL;
        contact_body_t *b;
        struct to_body *pto, *pfrom = NULL, TO;
        int lexpire;
@@ -1128,13 +1127,11 @@ int extract_sdialog_info(subs_t* subs,struct sip_msg* msg, int mexp,
        
        if((!scontact.s) || (scontact.len== 0))
        {
-               contact= get_local_contact(msg);
-               if(contact== NULL)
+               if(ps_fill_local_contact(msg, &subs->local_contact)<0)
                {
-                       LM_ERR("in function get_local_contact\n");
+                       LM_ERR("cannot get local contact address\n");
                        goto error;
                }
-               subs->local_contact= *contact;
        }
        else
                subs->local_contact= scontact;
index 077fef1..16e427d 100644 (file)
@@ -85,27 +85,19 @@ static inline int uandd_to_uri(str user,  str domain, str *out)
        return 0;
 }
 
-static inline str* get_local_contact(struct sip_msg* msg)
+static inline int ps_fill_local_contact(struct sip_msg* msg, str *contact)
 {
        str ip;
        char* proto;
        int port;
        int len;
-       str* contact;
        int plen;
 
-       contact= (str*)pkg_malloc(sizeof(str));
-       if(contact== NULL)
-       {
-               LM_ERR("No more memory\n");
-               return NULL;
-       }
        contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE);
        if(contact->s== NULL)
        {
                LM_ERR("No more memory\n");
-               pkg_free(contact);
-               return NULL;
+               goto error;
        }
 
        memset(contact->s, 0, LCONTACT_BUF_SIZE);
@@ -128,18 +120,14 @@ static inline str* get_local_contact(struct sip_msg* msg)
        else
        {
                LM_ERR("unsupported proto\n");
-               pkg_free(contact->s);
-               pkg_free(contact);
-               return NULL;
+               goto error;
        }       
        
        ip.s= ip_addr2a(&msg->rcv.dst_ip);
        if(ip.s== NULL)
        {
                LM_ERR("transforming ip_addr to ascii\n");
-               pkg_free(contact->s);
-               pkg_free(contact);
-               return NULL;
+               goto error;
        }
        ip.len= strlen(ip.s);
        port = msg->rcv.dst_port;
@@ -154,25 +142,26 @@ static inline str* get_local_contact(struct sip_msg* msg)
        if(contact->len> LCONTACT_BUF_SIZE - 21)
        {
                LM_ERR("buffer overflow\n");
-               pkg_free(contact->s);
-               pkg_free(contact);
-               return NULL;
+               goto error;
 
        }       
        len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
        if(len< 0)
        {
                LM_ERR("unsuccessful sprintf\n");
-               pkg_free(contact->s);
-               pkg_free(contact);
-               return NULL;
+               goto error;
        }       
        contact->len+= len;
        strncpy(contact->s+ contact->len, proto, plen);
        contact->len += plen;
        
-       return contact;
-       
+       return 0;
+error:
+       if(contact->s!=NULL)
+               pkg_free(contact->s);
+       contact->s = 0;
+       contact->len = 0;
+       return -1;
 }
 
 //str* int_to_str(long int n);