Module: sip-router Branch: master Commit: 4462757813b146e2ca4018b1a93636115e2408f6 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=44627578...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Thu Feb 3 23:54:13 2011 +0100
presence(k): avoid str* alloc for local contact
- it is not used further an leads to memleak
---
modules_k/presence/subscribe.c | 7 ++----- modules_k/presence/utils_func.h | 37 +++++++++++++------------------------ 2 files changed, 15 insertions(+), 29 deletions(-)
diff --git a/modules_k/presence/subscribe.c b/modules_k/presence/subscribe.c index e96efc0..d37518a 100644 --- a/modules_k/presence/subscribe.c +++ b/modules_k/presence/subscribe.c @@ -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; diff --git a/modules_k/presence/utils_func.h b/modules_k/presence/utils_func.h index 077fef1..16e427d 100644 --- a/modules_k/presence/utils_func.h +++ b/modules_k/presence/utils_func.h @@ -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);