From 34307d8e52d97298404ed5fe95e9e08932d2c0db Mon Sep 17 00:00:00 2001 From: Moises Silva Date: Mon, 16 Nov 2009 14:52:02 +0000 Subject: [PATCH] replaced a ton of free or strdup calls for zap_safe_free and zap_strdup git-svn-id: http://svn.openzap.org/svn/openzap/branches/sangoma_boost@864 a93c3328-9c30-0410-af19-c9cd2b2d52af --- libs/freetdm/src/fsk.c | 6 ++-- libs/freetdm/src/hashtable.c | 12 +++---- libs/freetdm/src/hashtable_itr.c | 2 +- libs/freetdm/src/include/openzap.h | 3 ++ libs/freetdm/src/libteletone_generate.c | 19 ++---------- .../src/ozmod/ozmod_libpri/ozmod_libpri.c | 2 +- .../freetdm/src/ozmod/ozmod_pika/ozmod_pika.c | 8 ++--- libs/freetdm/src/ozmod/ozmod_r2/ozmod_r2.c | 4 +-- .../src/ozmod/ozmod_wanpipe/ozmod_wanpipe.c | 4 +-- libs/freetdm/src/ozmod/ozmod_zt/ozmod_zt.c | 4 +-- libs/freetdm/src/uart.c | 2 +- libs/freetdm/src/zap_buffer.c | 6 ++-- libs/freetdm/src/zap_dso.c | 9 +++--- libs/freetdm/src/zap_io.c | 31 +++++++++++++------ libs/freetdm/src/zap_m3ua.c | 4 +-- libs/freetdm/src/zap_threadmutex.c | 6 ++-- 16 files changed, 62 insertions(+), 60 deletions(-) diff --git a/libs/freetdm/src/fsk.c b/libs/freetdm/src/fsk.c index f55d421559..16a1bcb7d4 100644 --- a/libs/freetdm/src/fsk.c +++ b/libs/freetdm/src/fsk.c @@ -226,13 +226,13 @@ void dsp_fsk_destroy(dsp_fsk_handle_t **handle) for (i = 0; i < 4; i++) { if ((*handle)->correlates[i] != NULL) { - free((*handle)->correlates[i]); + zap_safe_free((*handle)->correlates[i]); (*handle)->correlates[i] = NULL; } } if ((*handle)->buffer != NULL) { - free((*handle)->buffer); + zap_safe_free((*handle)->buffer); (*handle)->buffer = NULL; } @@ -241,7 +241,7 @@ void dsp_fsk_destroy(dsp_fsk_handle_t **handle) dsp_uart_destroy(dhandle); } - free(*handle); + zap_safe_free(*handle); *handle = NULL; } diff --git a/libs/freetdm/src/hashtable.c b/libs/freetdm/src/hashtable.c index 415dda7d8d..15ec9014f4 100644 --- a/libs/freetdm/src/hashtable.c +++ b/libs/freetdm/src/hashtable.c @@ -73,7 +73,7 @@ create_hashtable(unsigned int minsize, h = (struct hashtable *)zap_malloc(sizeof(struct hashtable)); if (NULL == h) return NULL; /*oom*/ h->table = (struct entry **)zap_malloc(sizeof(struct entry*) * size); - if (NULL == h->table) { free(h); return NULL; } /*oom*/ + if (NULL == h->table) { zap_safe_free(h); return NULL; } /*oom*/ memset(h->table, 0, size * sizeof(struct entry *)); h->tablelength = size; h->primeindex = pindex; @@ -125,7 +125,7 @@ hashtable_expand(struct hashtable *h) newtable[index] = e; } } - free(h->table); + zap_safe_free(h->table); h->table = newtable; } /* Plan B: realloc instead */ @@ -236,7 +236,7 @@ hashtable_remove(struct hashtable *h, void *k) if (e->flags & HASHTABLE_FLAG_FREE_KEY) { freekey(e->k); } - free(e); + zap_safe_free(e); return v; } pE = &(e->next); @@ -258,11 +258,11 @@ hashtable_destroy(struct hashtable *h) { e = table[i]; while (NULL != e) - { f = e; e = e->next; if (f->flags & HASHTABLE_FLAG_FREE_KEY) freekey(f->k); if (f->flags & HASHTABLE_FLAG_FREE_VALUE) free(f->v); free(f); } + { f = e; e = e->next; if (f->flags & HASHTABLE_FLAG_FREE_KEY) freekey(f->k); if (f->flags & HASHTABLE_FLAG_FREE_VALUE) zap_safe_free(f->v); zap_safe_free(f); } } - free(h->table); - free(h); + zap_safe_free(h->table); + zap_safe_free(h); } OZ_DECLARE(struct hashtable_iterator *) hashtable_next(struct hashtable_iterator *i) diff --git a/libs/freetdm/src/hashtable_itr.c b/libs/freetdm/src/hashtable_itr.c index cb8165dc91..ab5e1ce0c8 100644 --- a/libs/freetdm/src/hashtable_itr.c +++ b/libs/freetdm/src/hashtable_itr.c @@ -138,7 +138,7 @@ hashtable_iterator_remove(struct hashtable_itr *itr) remember_parent = itr->parent; ret = hashtable_iterator_advance(itr); if (itr->parent == remember_e) { itr->parent = remember_parent; } - free(remember_e); + zap_safe_free(remember_e); return ret; } diff --git a/libs/freetdm/src/include/openzap.h b/libs/freetdm/src/include/openzap.h index 726cee3b09..bc6b0a9dd3 100644 --- a/libs/freetdm/src/include/openzap.h +++ b/libs/freetdm/src/include/openzap.h @@ -625,6 +625,9 @@ OZ_DECLARE(zap_status_t) zap_queue_wait(zap_queue_t *queue, int ms); /*! destroy the queue */ OZ_DECLARE(zap_status_t) zap_queue_destroy(zap_queue_t **queue); +/* Duplicate string */ +OZ_DECLARE(char *) zap_strdup(const char *str); + OZ_DECLARE(zap_size_t) zap_fsk_modulator_generate_bit(zap_fsk_modulator_t *fsk_trans, int8_t bit, int16_t *buf, zap_size_t buflen); OZ_DECLARE(int32_t) zap_fsk_modulator_generate_carrier_bits(zap_fsk_modulator_t *fsk_trans, uint32_t bits); OZ_DECLARE(void) zap_fsk_modulator_generate_chan_sieze(zap_fsk_modulator_t *fsk_trans); diff --git a/libs/freetdm/src/libteletone_generate.c b/libs/freetdm/src/libteletone_generate.c index bb41224c9a..c261772f92 100644 --- a/libs/freetdm/src/libteletone_generate.c +++ b/libs/freetdm/src/libteletone_generate.c @@ -142,7 +142,7 @@ TELETONE_API(int) teletone_init_session(teletone_generation_session_t *ts, int b TELETONE_API(int) teletone_destroy_session(teletone_generation_session_t *ts) { if (ts->buffer) { - free(ts->buffer); + zap_safe_free(ts->buffer); ts->buffer = NULL; ts->samples = 0; } @@ -270,19 +270,6 @@ TELETONE_API(int) teletone_mux_tones(teletone_generation_session_t *ts, teletone return ts->samples; } -/* don't ask */ -static char *my_strdup (const char *s) -{ - size_t len = strlen (s) + 1; - void *new = zap_malloc(len); - - if (new == NULL) { - return NULL; - } - - return (char *) memcpy (new, s, len); -} - TELETONE_API(int) teletone_run(teletone_generation_session_t *ts, const char *cmd) { char *data = NULL, *cur = NULL, *end = NULL; @@ -293,7 +280,7 @@ TELETONE_API(int) teletone_run(teletone_generation_session_t *ts, const char *cm } do { - if (!(data = my_strdup(cmd))) { + if (!(data = zap_strdup(cmd))) { return -1; } @@ -442,7 +429,7 @@ TELETONE_API(int) teletone_run(teletone_generation_session_t *ts, const char *cm } } bottom: - free(data); + zap_safe_free(data); data = NULL; if (ts->LOOPS > 0) { ts->LOOPS--; diff --git a/libs/freetdm/src/ozmod/ozmod_libpri/ozmod_libpri.c b/libs/freetdm/src/ozmod/ozmod_libpri/ozmod_libpri.c index e80de2686d..ec92684a57 100644 --- a/libs/freetdm/src/ozmod/ozmod_libpri/ozmod_libpri.c +++ b/libs/freetdm/src/ozmod/ozmod_libpri/ozmod_libpri.c @@ -210,7 +210,7 @@ static ZIO_API_FUNCTION(zap_libpri_api) int argc = 0; if (data) { - mycmd = strdup(data); + mycmd = zap_strdup(data); argc = zap_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); } diff --git a/libs/freetdm/src/ozmod/ozmod_pika/ozmod_pika.c b/libs/freetdm/src/ozmod/ozmod_pika/ozmod_pika.c index 536110c32a..2932a342e7 100644 --- a/libs/freetdm/src/ozmod/ozmod_pika/ozmod_pika.c +++ b/libs/freetdm/src/ozmod/ozmod_pika/ozmod_pika.c @@ -358,7 +358,7 @@ static unsigned pika_open_range(zap_span_t *span, unsigned boardno, unsigned spa if (status != PK_SUCCESS) { zap_log(ZAP_LOG_ERROR, "Error: PKH_QUEUE_Create failed(%s)!\n", PKH_ERROR_GetText(status, error_text, sizeof(error_text))); - free(span_data); + zap_safe_free(span_data); return 0; } @@ -605,7 +605,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(pika_configure_span) assert(str != NULL); - mydata = strdup(str); + mydata = zap_strdup(str); assert(mydata != NULL); if ((profile_name = strchr(mydata, '@'))) { @@ -668,7 +668,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(pika_configure_span) } - free(mydata); + zap_safe_free(mydata); return configured; } @@ -1211,7 +1211,7 @@ static ZIO_SPAN_DESTROY_FUNCTION(pika_span_destroy) if (span_data) { PKH_QUEUE_Destroy(span_data->event_queue); - free(span_data); + zap_safe_free(span_data); } return ZAP_SUCCESS; diff --git a/libs/freetdm/src/ozmod/ozmod_r2/ozmod_r2.c b/libs/freetdm/src/ozmod/ozmod_r2/ozmod_r2.c index 837a8887b7..8000aa2a64 100644 --- a/libs/freetdm/src/ozmod/ozmod_r2/ozmod_r2.c +++ b/libs/freetdm/src/ozmod/ozmod_r2/ozmod_r2.c @@ -752,7 +752,7 @@ static ZIO_SIG_CONFIGURE_FUNCTION(zap_r2_configure_span) r2conf.loglevel |= tmplevel; zap_log(ZAP_LOG_DEBUG, "Configuring R2 span %d with loglevel %s\n", span->span_id, clevel); } - free(logval); + zap_safe_free(logval); } else if (!strcasecmp(var, "advanced_protocol_file")) { if (!(val = va_arg(ap, char *))) { break; @@ -1182,7 +1182,7 @@ static ZIO_API_FUNCTION(zap_r2_api) int argc = 0; if (data) { - mycmd = strdup(data); + mycmd = zap_strdup(data); argc = zap_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); } diff --git a/libs/freetdm/src/ozmod/ozmod_wanpipe/ozmod_wanpipe.c b/libs/freetdm/src/ozmod/ozmod_wanpipe/ozmod_wanpipe.c index a275acc352..3d681985fb 100644 --- a/libs/freetdm/src/ozmod/ozmod_wanpipe/ozmod_wanpipe.c +++ b/libs/freetdm/src/ozmod/ozmod_wanpipe/ozmod_wanpipe.c @@ -397,7 +397,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(wanpipe_configure_span) assert(str != NULL); - mydata = strdup(str); + mydata = zap_strdup(str); assert(mydata != NULL); @@ -447,7 +447,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(wanpipe_configure_span) } - free(mydata); + zap_safe_free(mydata); return configured; } diff --git a/libs/freetdm/src/ozmod/ozmod_zt/ozmod_zt.c b/libs/freetdm/src/ozmod/ozmod_zt/ozmod_zt.c index b8788d2a58..bbd64352f4 100644 --- a/libs/freetdm/src/ozmod/ozmod_zt/ozmod_zt.c +++ b/libs/freetdm/src/ozmod/ozmod_zt/ozmod_zt.c @@ -452,7 +452,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(zt_configure_span) assert(str != NULL); - mydata = strdup(str); + mydata = zap_strdup(str); assert(mydata != NULL); @@ -493,7 +493,7 @@ static ZIO_CONFIGURE_SPAN_FUNCTION(zt_configure_span) } - free(mydata); + zap_safe_free(mydata); return configured; diff --git a/libs/freetdm/src/uart.c b/libs/freetdm/src/uart.c index 14f066da59..b3728e6fc8 100644 --- a/libs/freetdm/src/uart.c +++ b/libs/freetdm/src/uart.c @@ -90,7 +90,7 @@ dsp_uart_handle_t *dsp_uart_create(dsp_uart_attr_t *attr) void dsp_uart_destroy(dsp_uart_handle_t **handle) { if (*handle) { - free(*handle); + zap_safe_free(*handle); *handle = NULL; } } diff --git a/libs/freetdm/src/zap_buffer.c b/libs/freetdm/src/zap_buffer.c index bd8b0cc307..11a7c03d29 100644 --- a/libs/freetdm/src/zap_buffer.c +++ b/libs/freetdm/src/zap_buffer.c @@ -60,7 +60,7 @@ OZ_DECLARE(zap_status_t) zap_buffer_create(zap_buffer_t **buffer, zap_size_t blo if (start_len) { new_buffer->data = zap_malloc(start_len); if (!new_buffer->data) { - free(new_buffer); + zap_safe_free(new_buffer); return ZAP_MEMERR; } memset(new_buffer->data, 0, start_len); @@ -283,8 +283,8 @@ OZ_DECLARE(zap_size_t) zap_buffer_zwrite(zap_buffer_t *buffer, const void *data, OZ_DECLARE(void) zap_buffer_destroy(zap_buffer_t **buffer) { if (*buffer) { - free((*buffer)->data); - free(*buffer); + zap_safe_free((*buffer)->data); + zap_safe_free(*buffer); } *buffer = NULL; diff --git a/libs/freetdm/src/zap_dso.c b/libs/freetdm/src/zap_dso.c index ead40e7c4b..eefe6e819c 100644 --- a/libs/freetdm/src/zap_dso.c +++ b/libs/freetdm/src/zap_dso.c @@ -17,6 +17,7 @@ * */ +#include "openzap.h" #include "zap_dso.h" #include #include @@ -46,7 +47,7 @@ zap_dso_lib_t zap_dso_open(const char *path, char **err) { DWORD error = GetLastError(); char tmp[80]; sprintf(tmp, "dll open error [%ul]\n", error); - *err = _strdup(tmp); + *err = zap_strdup(tmp); } return lib; @@ -58,7 +59,7 @@ void* zap_dso_func_sym(zap_dso_lib_t lib, const char *sym, char **err) { DWORD error = GetLastError(); char tmp[80]; sprintf(tmp, "dll sym error [%ul]\n", error); - *err = _strdup(tmp); + *err = zap_strdup(tmp); } return (void *)(intptr_t)func; // this should really be addr - zap_dso_func_data } @@ -87,7 +88,7 @@ void zap_dso_destroy(zap_dso_lib_t *lib) { zap_dso_lib_t zap_dso_open(const char *path, char **err) { void *lib = dlopen(path, RTLD_NOW | RTLD_LOCAL); if (lib == NULL) { - *err = strdup(dlerror()); + *err = zap_strdup(dlerror()); } return lib; } @@ -95,7 +96,7 @@ zap_dso_lib_t zap_dso_open(const char *path, char **err) { void *zap_dso_func_sym(zap_dso_lib_t lib, const char *sym, char **err) { void *func = dlsym(lib, sym); if (!func) { - *err = strdup(dlerror()); + *err = zap_strdup(dlerror()); } return func; } diff --git a/libs/freetdm/src/zap_io.c b/libs/freetdm/src/zap_io.c index d0a6aaf69d..a8e7329558 100644 --- a/libs/freetdm/src/zap_io.c +++ b/libs/freetdm/src/zap_io.c @@ -452,7 +452,7 @@ OZ_DECLARE(zap_status_t) zap_span_create(zap_io_interface_t *zio, zap_span_t **s snprintf(buf, sizeof(buf), "span%d", new_span->span_id); name = buf; } - new_span->name = strdup(name); + new_span->name = zap_strdup(name); zap_span_add(new_span); *span = new_span; status = ZAP_SUCCESS; @@ -2306,8 +2306,8 @@ OZ_DECLARE(zap_status_t) zap_channel_add_var(zap_channel_t *zchan, const char *v return ZAP_FAIL; } - t_name = strdup(var_name); - t_val = strdup(value); + t_name = zap_strdup(var_name); + t_val = zap_strdup(value); if(hashtable_insert(zchan->variable_hash, t_name, t_val, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_FLAG_FREE_VALUE)) { return ZAP_SUCCESS; @@ -2336,7 +2336,7 @@ OZ_DECLARE(char *) zap_api_execute(const char *type, const char *cmd) char *rval = NULL; if (type && !cmd) { - dup = strdup(type); + dup = zap_strdup(type); if ((p = strchr(dup, ' '))) { *p++ = '\0'; cmd = p; @@ -2440,7 +2440,7 @@ static zap_status_t load_config(void) } if (zap_span_create(zio, &span, name) == ZAP_SUCCESS) { - span->type = strdup(type); + span->type = zap_strdup(type); d = 0; zap_log(ZAP_LOG_DEBUG, "created span %d (%s) of type %s\n", span->span_id, span->name, type); @@ -2534,7 +2534,7 @@ static zap_status_t load_config(void) } else if (!strcasecmp(var, "cas-channel")) { configured += zio->configure_span(span, val, ZAP_CHAN_TYPE_CAS, name, number); } else if (!strcasecmp(var, "dtmf_hangup")) { - span->dtmf_hangup = strdup(val); + span->dtmf_hangup = zap_strdup(val); span->dtmf_hangup_len = strlen(val); } else { zap_log(ZAP_LOG_ERROR, "unknown span variable '%s'\n", var); @@ -2849,7 +2849,7 @@ OZ_DECLARE(zap_status_t) zap_global_destroy(void) if (zap_test_flag(cur_chan, ZAP_CHANNEL_CONFIGURED)) { zap_channel_destroy(cur_chan); } - free(cur_chan); + zap_safe_free(cur_chan); cur_chan = NULL; } } @@ -2866,7 +2866,7 @@ OZ_DECLARE(zap_status_t) zap_global_destroy(void) hashtable_remove(globals.span_hash, (void *)cur_span->name); zap_safe_free(cur_span->type); zap_safe_free(cur_span->name); - free(cur_span); + zap_safe_free(cur_span); cur_span = NULL; } } @@ -3157,7 +3157,7 @@ OZ_DECLARE_NONSTD(zap_status_t) zap_console_stream_write(zap_stream_handle_t *ha end = handle->end; } else { zap_log(ZAP_LOG_CRIT, "Memory Error!\n"); - free(data); + zap_safe_free(data); return ZAP_FAIL; } } @@ -3170,12 +3170,23 @@ OZ_DECLARE_NONSTD(zap_status_t) zap_console_stream_write(zap_stream_handle_t *ha handle->data_len = strlen(buf); handle->end = (uint8_t *) (handle->data) + handle->data_len; } - free(data); + zap_safe_free(data); } return ret ? ZAP_FAIL : ZAP_SUCCESS; } +OZ_DECLARE(char *) zap_strdup(const char *str) +{ + zap_size_t len = strlen(str) + 1; + void *new = zap_malloc(len); + + if (!new) { + return NULL; + } + + return (char *)memcpy(new, str, len); +} /* For Emacs: diff --git a/libs/freetdm/src/zap_m3ua.c b/libs/freetdm/src/zap_m3ua.c index 8e572c92d7..bcc0e0559d 100644 --- a/libs/freetdm/src/zap_m3ua.c +++ b/libs/freetdm/src/zap_m3ua.c @@ -487,7 +487,7 @@ static ZIO_SPAN_DESTROY_FUNCTION(m3ua_span_destroy) m3ua_span_data_t *span_data = (m3ua_span_data_t *) span->mod_data; if (span_data) { - free(span_data); + zap_safe_free(span_data); } return ZAP_SUCCESS; @@ -513,7 +513,7 @@ static ZIO_CHANNEL_DESTROY_FUNCTION(m3ua_channel_destroy) zap_safe_free(chan_data); if (span_data) { - free(span_data); + zap_safe_free(span_data); } diff --git a/libs/freetdm/src/zap_threadmutex.c b/libs/freetdm/src/zap_threadmutex.c index d4a12f4186..1fb3c479f6 100644 --- a/libs/freetdm/src/zap_threadmutex.c +++ b/libs/freetdm/src/zap_threadmutex.c @@ -84,7 +84,7 @@ static void * ZAP_THREAD_CALLING_CONVENTION thread_launch(void *args) #ifndef WIN32 pthread_attr_destroy(&thread->attribute); #endif - free(thread); + zap_safe_free(thread); return exit_val; } @@ -134,7 +134,7 @@ OZ_DECLARE(zap_status_t) zap_thread_create_detached_ex(zap_thread_function_t fun fail: if (thread) { - free(thread); + zap_safe_free(thread); } done: return status; @@ -192,7 +192,7 @@ OZ_DECLARE(zap_status_t) zap_mutex_destroy(zap_mutex_t **mutex) if (pthread_mutex_destroy(&mp->mutex)) return ZAP_FAIL; #endif - free(mp); + zap_safe_free(mp); return ZAP_SUCCESS; }