From bc6334a6101e32c0444f5880f50b366916f57bc4 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Mon, 7 Jun 2010 16:47:35 -0500 Subject: [PATCH] FSCORE-617 --- src/include/switch_mprintf.h | 6 +++--- src/include/switch_utils.h | 9 ++++++--- src/mod/applications/mod_commands/mod_commands.c | 4 ++-- src/mod/applications/mod_directory/mod_directory.c | 2 +- src/mod/applications/mod_dptools/mod_dptools.c | 2 +- src/mod/endpoints/mod_dingaling/mod_dingaling.c | 12 ++++++------ src/mod/endpoints/mod_skinny/mod_skinny.c | 2 +- src/switch_core_db.c | 2 +- src/switch_core_sqldb.c | 10 ++++++++-- 9 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/include/switch_mprintf.h b/src/include/switch_mprintf.h index af2396800d..cc5ed337d5 100644 --- a/src/include/switch_mprintf.h +++ b/src/include/switch_mprintf.h @@ -22,7 +22,7 @@ SWITCH_BEGIN_EXTERN_C * options that are useful for constructing SQL statements. * * The strings returned by this routine should be freed by calling - * switch_core_db_free(). + * free(). * * All of the usual printf formatting options apply. In addition, there * is a "%q" option. %q works like %s in that it substitutes a null-terminated @@ -37,9 +37,9 @@ SWITCH_BEGIN_EXTERN_C * * We can use this text in an SQL statement as follows: * - * char *z = switch_core_db_mprintf("INSERT INTO TABLES('%q')", zText); + * char *z = switch_mprintf("INSERT INTO TABLES('%q')", zText); * switch_core_db_exec(db, z, callback1, 0, 0); - * switch_core_db_free(z); + * free(z); * * Because the %q format string is used, the '\'' character in zText * is escaped and the SQL generated is as follows: diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index cea216a7e4..4585385949 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -653,14 +653,17 @@ SWITCH_DECLARE(int) switch_split_user_domain(char *in, char **user, char **domai /* malloc or DIE macros */ #ifdef NDEBUG #define switch_malloc(ptr, len) (void)( (!!(ptr = malloc(len))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), ptr ) -#define switch_zmalloc(ptr, len) (void)( (!!(ptr = malloc(len))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), memset(ptr, 0, len)) +#define switch_zmalloc(ptr, len) (void)( (!!(ptr = calloc(1, (len)))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), ptr) +#define switch_strdup(ptr, s) (void)( (!!(ptr = strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), ptr) #else #if (_MSC_VER >= 1500) // VC9+ #define switch_malloc(ptr, len) (void)(assert(((ptr) = malloc((len)))),ptr);__analysis_assume( ptr ) -#define switch_zmalloc(ptr, len) (void)(assert((ptr = malloc(len))),memset(ptr, 0, len));__analysis_assume( ptr ) +#define switch_zmalloc(ptr, len) (void)(assert((ptr = calloc(1, (len)))),ptr);__analysis_assume( ptr ) +#define switch_strdup(ptr, s) (void)(assert(((ptr) = _strdup(s)((len)))),ptr);__analysis_assume( ptr ) #else #define switch_malloc(ptr, len) (void)(assert(((ptr) = malloc((len)))),ptr) -#define switch_zmalloc(ptr, len) (void)(assert((ptr = malloc(len))),memset(ptr, 0, len)) +#define switch_zmalloc(ptr, len) (void)(assert((ptr = calloc(1, (len)))),ptr) +#define switch_strdup(ptr, s) (void)(assert(((ptr) = strdup((s)))),ptr) #endif #endif diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 2acd254c2d..990eb131e6 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -3500,7 +3500,7 @@ SWITCH_STANDARD_API(show_function) if (errmsg) { stream->write_function(stream, "-ERR SQL Error [%s]\n", errmsg); - switch_core_db_free(errmsg); + free(errmsg); errmsg = NULL; } else if (help) { if (holder.count == 0) @@ -3513,7 +3513,7 @@ SWITCH_STANDARD_API(show_function) if (errmsg) { stream->write_function(stream, "-ERR SQL Error [%s]\n", errmsg); - switch_core_db_free(errmsg); + free(errmsg); errmsg = NULL; } diff --git a/src/mod/applications/mod_directory/mod_directory.c b/src/mod/applications/mod_directory/mod_directory.c index 3a09216877..d3389ab3b1 100644 --- a/src/mod/applications/mod_directory/mod_directory.c +++ b/src/mod/applications/mod_directory/mod_directory.c @@ -295,7 +295,7 @@ static switch_bool_t directory_execute_sql_callback(switch_mutex_t *mutex, char if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); - free(errmsg); + switch_core_db_free(errmsg); } if (db) { diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index 7947526383..7ca52419f0 100755 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -379,7 +379,7 @@ SWITCH_STANDARD_APP(eavesdrop_function) switch_cache_db_release_db_handle(&db); if (errmsg) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error: %s\n", errmsg); - switch_core_db_free(errmsg); + free(errmsg); if ((file = switch_channel_get_variable(channel, "eavesdrop_indicate_failed"))) { switch_ivr_play_file(session, NULL, file, NULL); } diff --git a/src/mod/endpoints/mod_dingaling/mod_dingaling.c b/src/mod/endpoints/mod_dingaling/mod_dingaling.c index aa3f07a959..27cf1cf005 100644 --- a/src/mod/endpoints/mod_dingaling/mod_dingaling.c +++ b/src/mod/endpoints/mod_dingaling/mod_dingaling.c @@ -317,7 +317,7 @@ static switch_bool_t mdl_execute_sql_callback(mdl_profile_t *profile, if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); - free(errmsg); + switch_core_db_free(errmsg); } if (db) { @@ -2730,7 +2730,7 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi if ((sql = switch_mprintf("delete from jabber_subscriptions where sub_from='%q' and sub_to='%q';", from, to))) { mdl_execute_sql(profile, sql, profile->mutex); - switch_core_db_free(sql); + free(sql); } break; @@ -2743,12 +2743,12 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi switch_mutex_lock(profile->mutex); if ((sql = switch_mprintf("delete from jabber_subscriptions where sub_from='%q' and sub_to='%q'", from, to))) { mdl_execute_sql(profile, sql, NULL); - switch_core_db_free(sql); + free(sql); } if ((sql = switch_mprintf("insert into jabber_subscriptions values('%q','%q','%q','%q');\n", switch_str_nil(from), switch_str_nil(to), switch_str_nil(msg), switch_str_nil(subject)))) { mdl_execute_sql(profile, sql, NULL); - switch_core_db_free(sql); + free(sql); } switch_mutex_unlock(profile->mutex); if (is_special(to)) { @@ -2796,7 +2796,7 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi if ((sql = switch_mprintf("update jabber_subscriptions set show_pres='%q', status='%q' where sub_from='%q'", switch_str_nil(msg), switch_str_nil(subject), switch_str_nil(from)))) { mdl_execute_sql(profile, sql, profile->mutex); - switch_core_db_free(sql); + free(sql); } if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) { @@ -2831,7 +2831,7 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi if ((sql = switch_mprintf("update jabber_subscriptions set show_pres='%q', status='%q' where sub_from='%q'", switch_str_nil(msg), switch_str_nil(subject), switch_str_nil(from)))) { mdl_execute_sql(profile, sql, profile->mutex); - switch_core_db_free(sql); + free(sql); } if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_OUT) == SWITCH_STATUS_SUCCESS) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", MDL_CHAT_PROTO); diff --git a/src/mod/endpoints/mod_skinny/mod_skinny.c b/src/mod/endpoints/mod_skinny/mod_skinny.c index 444bb038d0..969992232a 100644 --- a/src/mod/endpoints/mod_skinny/mod_skinny.c +++ b/src/mod/endpoints/mod_skinny/mod_skinny.c @@ -329,7 +329,7 @@ switch_bool_t skinny_execute_sql_callback(skinny_profile_t *profile, if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); - free(errmsg); + switch_core_db_free(errmsg); } if (db) { diff --git a/src/switch_core_db.c b/src/switch_core_db.c index b3cfea9cfb..50ccbaf225 100644 --- a/src/switch_core_db.c +++ b/src/switch_core_db.c @@ -93,7 +93,7 @@ SWITCH_DECLARE(int) switch_core_db_exec(switch_core_db_t *db, const char *sql, s ret = sqlite3_exec(db, sql, callback, data, &err); if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) { if (sane > 1) { - switch_safe_free(err); + switch_core_db_free(err); switch_yield(100000); continue; } diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 061c77fccc..ee112d7a9b 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -374,6 +374,7 @@ static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t { switch_status_t status = SWITCH_STATUS_FALSE; char *errmsg = NULL; + char *tmp = NULL; if (dbh->io_mutex) { switch_mutex_lock(dbh->io_mutex); @@ -395,6 +396,11 @@ static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t case SCDB_TYPE_CORE_DB: { status = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, NULL, NULL, &errmsg); + if (errmsg) { + switch_strdup(tmp, errmsg); + switch_core_db_free(errmsg); + errmsg = tmp; + } } break; } @@ -721,7 +727,7 @@ SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cach if (errmsg) { dbh->last_used = switch_epoch_time_now(NULL) - (SQL_CACHE_TIMEOUT * 2); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); - free(errmsg); + switch_core_db_free(errmsg); } } break; @@ -874,7 +880,7 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread, len += newlen; } - switch_core_db_free(sql); + free(sql); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "SQL thread ending\n"); break;