From 7803be8ee42ad67cee69a9eaffbc2fafe07bf14f Mon Sep 17 00:00:00 2001 From: dvossel Date: Thu, 18 Jun 2009 16:37:42 +0000 Subject: [PATCH] fixes some memory leaks and redundant conditions (closes issue #15269) Reported by: contactmayankjain Patches: patch.txt uploaded by contactmayankjain (license 740) memory_leak_stuff.trunk.diff uploaded by dvossel (license 671) Tested by: contactmayankjain, dvossel git-svn-id: http://svn.digium.com/svn/asterisk/trunk@201678 f38db490-d61c-443f-a65b-d21fe96a405b --- apps/app_rpt.c | 24 +++++++----------------- channels/h323/ast_h323.cxx | 17 ++++++++--------- channels/misdn/isdn_lib.c | 4 ++++ channels/xpmr/xpmr.c | 2 +- codecs/gsm/src/gsm_destroy.c | 2 +- main/ast_expr2.c | 1 + main/ast_expr2f.c | 5 ++--- main/asterisk.c | 3 +-- pbx/pbx_config.c | 9 +++------ res/ael/ael_lex.c | 6 ++---- res/res_config_ldap.c | 18 ++++++------------ utils/ael_main.c | 3 +-- utils/conf2ael.c | 3 ++- utils/extconf.c | 3 +-- utils/stereorize.c | 3 +++ 15 files changed, 43 insertions(+), 60 deletions(-) diff --git a/apps/app_rpt.c b/apps/app_rpt.c index 14414140c..3ff722ad7 100644 --- a/apps/app_rpt.c +++ b/apps/app_rpt.c @@ -3937,7 +3937,7 @@ struct rpt_tele *tlist; struct rpt *myrpt; struct rpt_link *l,*l1,linkbase; struct ast_channel *mychannel; -int id_malloc, vmajor, vminor, m; +int vmajor, vminor, m; char *p,*ct,*ct_copy,*ident, *nodename,*cp; time_t t; #ifdef NEW_ASTERISK @@ -3984,14 +3984,10 @@ struct dahdi_params par; ast_free(mytele); pthread_exit(NULL); } - else{ - id_malloc = 1; - } } else { ident = ""; - id_malloc = 0; } rpt_mutex_unlock(&myrpt->lock); @@ -4007,8 +4003,7 @@ struct dahdi_params par; ast_log(LOG_NOTICE,"Telemetry thread aborted at line %d, mode: %d\n",__LINE__, mytele->mode); /*@@@@@@@@@@@*/ rpt_mutex_unlock(&myrpt->lock); ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); pthread_exit(NULL); } @@ -4054,8 +4049,7 @@ struct dahdi_params par; rpt_mutex_unlock(&myrpt->lock); ast_log(LOG_NOTICE,"Telemetry thread aborted at line %d, mode: %d\n",__LINE__, mytele->mode); /*@@@@@@@@@@@*/ ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); ast_hangup(mychannel); pthread_exit(NULL); @@ -4249,8 +4243,7 @@ struct dahdi_params par; rpt_mutex_unlock(&myrpt->lock); ast_log(LOG_NOTICE,"Telemetry thread aborted at line %d, mode: %d\n",__LINE__, mytele->mode); /*@@@@@@@@@@@*/ ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); ast_hangup(mychannel); pthread_exit(NULL); @@ -4290,8 +4283,7 @@ struct dahdi_params par; rpt_mutex_unlock(&myrpt->lock); ast_log(LOG_NOTICE,"Telemetry thread aborted at line %d, mode: %d\n",__LINE__, mytele->mode); /*@@@@@@@@@@@*/ ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); ast_hangup(mychannel); pthread_exit(NULL); @@ -4813,8 +4805,7 @@ struct dahdi_params par; rpt_mutex_unlock(&myrpt->lock); ast_log(LOG_NOTICE,"Telemetry thread aborted at line %d, mode: %d\n",__LINE__, mytele->mode); /*@@@@@@@@@@@*/ ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); ast_hangup(mychannel); pthread_exit(NULL); @@ -5190,8 +5181,7 @@ struct dahdi_params par; myrpt->active_telem = NULL; rpt_mutex_unlock(&myrpt->lock); ast_free(nodename); - if(id_malloc) - ast_free(ident); + ast_free(ident); ast_free(mytele); ast_hangup(mychannel); #ifdef APP_RPT_LOCK_DEBUG diff --git a/channels/h323/ast_h323.cxx b/channels/h323/ast_h323.cxx index aeecfdab5..e98592d7d 100644 --- a/channels/h323/ast_h323.cxx +++ b/channels/h323/ast_h323.cxx @@ -572,8 +572,7 @@ MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference, { #ifdef H323_H450 /* Dispatcher will free out all registered handlers */ - if (h450dispatcher) - delete h450dispatcher; + delete h450dispatcher; h450dispatcher = new H450xDispatcher(*this); h4502handler = new H4502Handler(*this, *h450dispatcher); h4504handler = new MyH4504Handler(*this, *h450dispatcher); @@ -1988,8 +1987,9 @@ void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, in if (cap && cap->IsUsable(*this)) { lastcap++; lastcap = localCapabilities.SetCapability(0, lastcap, cap); - } else if (cap) + } else { delete cap; /* Capability is not usable */ + } dtmfMode = dtmf_mode; if (h323debug) { @@ -2001,8 +2001,9 @@ void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, in cap = new H323_UserInputCapability(H323_UserInputCapability::BasicString); if (cap && cap->IsUsable(*this)) { lastcap = localCapabilities.SetCapability(0, lastcap, cap); - } else if (cap) + } else { delete cap; /* Capability is not usable */ + } sendUserInputMode = SendUserInputAsString; } else { if ((dtmfMode & H323_DTMF_RFC2833) != 0) { @@ -2011,8 +2012,7 @@ void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, in lastcap = localCapabilities.SetCapability(0, lastcap, cap); else { dtmfMode |= H323_DTMF_SIGNAL; - if (cap) - delete cap; /* Capability is not usable */ + delete cap; /* Capability is not usable */ } } if ((dtmfMode & H323_DTMF_CISCO) != 0) { @@ -2024,8 +2024,7 @@ void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, in dtmfMode |= H323_DTMF_SIGNAL; } else { dtmfMode |= H323_DTMF_SIGNAL; - if (cap) - delete cap; /* Capability is not usable */ + delete cap; /* Capability is not usable */ } } if ((dtmfMode & H323_DTMF_SIGNAL) != 0) { @@ -2033,7 +2032,7 @@ void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, in cap = new H323_UserInputCapability(H323_UserInputCapability::SignalToneH245); if (cap && cap->IsUsable(*this)) lastcap = localCapabilities.SetCapability(0, lastcap, cap); - else if (cap) + else delete cap; /* Capability is not usable */ } sendUserInputMode = SendUserInputAsTone; /* RFC2833 transmission handled at Asterisk level */ diff --git a/channels/misdn/isdn_lib.c b/channels/misdn/isdn_lib.c index ec8f37f09..47da58378 100644 --- a/channels/misdn/isdn_lib.c +++ b/channels/misdn/isdn_lib.c @@ -2341,6 +2341,10 @@ static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2) if (!rx || !tx) { cb_log(0,0,"Couldn't open files: %s\n",strerror(errno)); + if (rx) + fclose(rx); + if (tx) + fclose(tx); return ; } diff --git a/channels/xpmr/xpmr.c b/channels/xpmr/xpmr.c index c2742cbc3..7c87410b2 100755 --- a/channels/xpmr/xpmr.c +++ b/channels/xpmr/xpmr.c @@ -157,7 +157,7 @@ i16 string_parse(char *src, char **dest, char ***ptrs) TRACEJ(2,(" source len = %i\n",slen)); pd=*dest; - if(pd) free(pd); + free(pd); pd=calloc(slen+1,1); memcpy(pd,src,slen); *dest=pd; diff --git a/codecs/gsm/src/gsm_destroy.c b/codecs/gsm/src/gsm_destroy.c index 4807c0acd..b2748fc97 100644 --- a/codecs/gsm/src/gsm_destroy.c +++ b/codecs/gsm/src/gsm_destroy.c @@ -22,5 +22,5 @@ void gsm_destroy P1((S), gsm S) { - if (S) free((char *)S); + free((char *)S); } diff --git a/main/ast_expr2.c b/main/ast_expr2.c index 4dcda2e3b..d9abfc10f 100644 --- a/main/ast_expr2.c +++ b/main/ast_expr2.c @@ -2415,6 +2415,7 @@ static void free_value (struct val *vp) { if (vp==NULL) { + free(vp); return; } if (vp->type == AST_EXPR_string || vp->type == AST_EXPR_numeric_string) diff --git a/main/ast_expr2f.c b/main/ast_expr2f.c index 15dbb5c2e..db09f28de 100644 --- a/main/ast_expr2f.c +++ b/main/ast_expr2f.c @@ -2379,7 +2379,7 @@ int ast_yyerror(const char *, YYLTYPE *, struct parse_io *); /* likewise */ void ast_yyfree(void *ptr, yyscan_t yyscanner) { - if (ptr) /* the normal generated ast_yyfree func just frees its first arg; + /* the normal generated ast_yyfree func just frees its first arg; this get complaints on some systems, as sometimes this arg is a nil ptr! It's usually not fatal, but is irritating! */ free( (char *) ptr ); @@ -2416,8 +2416,7 @@ int ast_expr(char *expr, char *buf, int length, struct ast_channel *chan) else buf[0] = 0; return_value = strlen(buf); - if (io.val->u.s) - free(io.val->u.s); + free(io.val->u.s); } free(io.val); } diff --git a/main/asterisk.c b/main/asterisk.c index a756b305b..e83534787 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -941,8 +941,7 @@ void ast_unregister_atexit(void (*func)(void)) AST_RWLIST_TRAVERSE_SAFE_END; AST_RWLIST_UNLOCK(&atexits); - if (ae) - free(ae); + free(ae); } /* Sending commands from consoles back to the daemon requires a terminating NULL */ diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c index 8bebf1385..cb89fd7d4 100644 --- a/pbx/pbx_config.c +++ b/pbx/pbx_config.c @@ -451,8 +451,7 @@ static char *complete_dialplan_remove_extension(struct ast_cli_args *a) ast_unlock_contexts(); error2: - if (exten) - free(exten); + free(exten); } else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */ char *exten = NULL, *context, *cid, *p; struct ast_context *c; @@ -509,8 +508,7 @@ static char *complete_dialplan_remove_extension(struct ast_cli_args *a) } ast_unlock_contexts(); error3: - if (exten) - free(exten); + free(exten); } return ret; } @@ -1138,8 +1136,7 @@ static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a) ret = strdup(ast_get_context_name(c)); } - if (ignorepat) - free(ignorepat); + free(ignorepat); ast_unlock_contexts(); return ret; } diff --git a/res/ael/ael_lex.c b/res/ael/ael_lex.c index 4e792aefe..6517644bd 100644 --- a/res/ael/ael_lex.c +++ b/res/ael/ael_lex.c @@ -3221,8 +3221,7 @@ static void pbcpush(char x) void ael_yyfree(void *ptr, yyscan_t yyscanner) { - if (ptr) - free( (char*) ptr ); + free( (char*) ptr ); } static int pbcpop(char x) @@ -3361,8 +3360,7 @@ struct pval *ael2_parse(char *filename, int *errors) *errors = 1; return 0; } - if (my_file) - free(my_file); + free(my_file); my_file = strdup(filename); stat(filename, &stats); buffer = (char*)malloc(stats.st_size+2); diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c index 770188fba..e0cb5946b 100644 --- a/res/res_config_ldap.c +++ b/res/res_config_ldap.c @@ -1271,10 +1271,8 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a ldap_err2string(result)); ast_mutex_unlock(&ldap_lock); - if (filter) - free(filter); - if (clean_basedn) - free(clean_basedn); + free(filter); + free(clean_basedn); ldap_msgfree(ldap_result_msg); ldap_mods_free(ldap_mods, 0); return -1; @@ -1297,10 +1295,8 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a } ast_mutex_unlock(&ldap_lock); - if (filter) - free(filter); - if (clean_basedn) - free(clean_basedn); + free(filter); + free(clean_basedn); ldap_msgfree(ldap_result_msg); ldap_mods_free(ldap_mods, 0); return num_entries; @@ -1458,10 +1454,8 @@ static int update2_ldap(const char *basedn, const char *table_name, va_list ap) ldap_err2string(result)); ast_mutex_unlock(&ldap_lock); - if (filter) - free(filter); - if (clean_basedn) - free(clean_basedn); + free(filter); + free(clean_basedn); ldap_msgfree(ldap_result_msg); ldap_mods_free(ldap_mods, 0); return -1; diff --git a/utils/ael_main.c b/utils/ael_main.c index ba7970955..9249cd77b 100644 --- a/utils/ael_main.c +++ b/utils/ael_main.c @@ -299,8 +299,7 @@ int ast_add_extension2(struct ast_context *con, } /* since add_extension2 is responsible for the malloc'd data stuff */ - if( data ) - free(data); + free(data); return 0; } diff --git a/utils/conf2ael.c b/utils/conf2ael.c index f08020686..48a18392c 100644 --- a/utils/conf2ael.c +++ b/utils/conf2ael.c @@ -471,7 +471,7 @@ int main(int argc, char **argv) if (mon) { *mon++ = 0; /* now all 4 fields are set; what do we do? */ - pvalIncludesAddIncludeWithTimeConstraints(incl, all, hr, dow, dom, mon); + pvalIncludesAddIncludeWithTimeConstraints(incl, strdup(all), strdup(hr), strdup(dow), strdup(dom), strdup(mon)); /* the original data is always best to keep (no 2-min rounding) */ } else { ast_log(LOG_ERROR,"No month spec attached to include!\n"); @@ -483,6 +483,7 @@ int main(int argc, char **argv) ast_log(LOG_ERROR,"No day of week spec attached to include!\n"); } } + free(all); } tmpi = tmpi->next; } diff --git a/utils/extconf.c b/utils/extconf.c index 11d4de60a..08465c408 100644 --- a/utils/extconf.c +++ b/utils/extconf.c @@ -2993,8 +2993,7 @@ static struct ast_var_t *ast_var_assign(const char *name, const char *value) static void ast_var_delete(struct ast_var_t *var) { - if (var) - free(var); + free(var); } diff --git a/utils/stereorize.c b/utils/stereorize.c index c8428320d..31f1b1fbe 100644 --- a/utils/stereorize.c +++ b/utils/stereorize.c @@ -156,4 +156,7 @@ int main( int argcount, char *args[]) } } /* That was an endless loop. This point is never reached. */ + free(leftsample); + free(rightsample); + free(stereosample); }