resolves MDXMLINT-11 and MDXMLINT-12, re-factors error handling and cleanup to eliminate possible segfaults and leaks.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5753 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2007-09-25 01:55:05 +00:00
parent 5e14d2f54c
commit 2066263064
1 changed files with 44 additions and 29 deletions

View File

@ -59,23 +59,25 @@ static void httpCallBack()
static switch_status_t my_on_hangup(switch_core_session_t *session) static switch_status_t my_on_hangup(switch_core_session_t *session)
{ {
switch_xml_t cdr; switch_xml_t cdr;
char *xml_text; char *xml_text = NULL;
char *path; char *path = NULL;
char *curl_xml_text = NULL;
char *logdir = NULL;
char *xml_text_escaped = NULL;
int fd = -1; int fd = -1;
uint32_t curTry; uint32_t curTry;
long httpRes; long httpRes;
CURL *curl_handle = NULL; CURL *curl_handle = NULL;
char *curl_xml_text;
char *logdir;
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
uint32_t i; uint32_t i;
switch_status_t status = SWITCH_STATUS_FALSE;
if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) { if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
/* build the XML */ /* build the XML */
if(!(xml_text = switch_mprintf("<?xml version=\"1.0\"?>\n%s",switch_xml_toxml(cdr)) )) { if(!(xml_text = switch_mprintf("<?xml version=\"1.0\"?>\n%s",switch_xml_toxml(cdr)) )) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
return SWITCH_STATUS_FALSE; goto error;
} }
/* do we log to the disk no matter what? */ /* do we log to the disk no matter what? */
@ -101,17 +103,24 @@ static switch_status_t my_on_hangup(switch_core_session_t *session)
#endif #endif
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error![%s]\n", ebuf); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error![%s]\n", ebuf);
} }
free(path); switch_safe_free(path);
} }
} }
/* try to post it to the web server */ /* try to post it to the web server */
if (!switch_strlen_zero(globals.url)) { if (!switch_strlen_zero(globals.url)) {
curl_handle = curl_easy_init(); curl_handle = curl_easy_init();
if(!(curl_xml_text = switch_mprintf("cdr=%s",xml_text) )) {
xml_text_escaped = curl_easy_escape(curl_handle, (const char*) xml_text, (int)strlen(xml_text));
if (switch_strlen_zero(xml_text_escaped)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
free(xml_text); goto error;
return SWITCH_STATUS_FALSE; }
if (!(curl_xml_text = switch_mprintf("cdr=%s", xml_text_escaped))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
goto error;
} }
if (!switch_strlen_zero(globals.cred)) { if (!switch_strlen_zero(globals.cred)) {
@ -134,12 +143,9 @@ static switch_status_t my_on_hangup(switch_core_session_t *session)
curl_easy_perform(curl_handle); curl_easy_perform(curl_handle);
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,&httpRes); curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,&httpRes);
if (httpRes==200) { if (httpRes==200) {
curl_easy_cleanup(curl_handle); goto success;
free(curl_xml_text);
free(xml_text);
return SWITCH_STATUS_SUCCESS;
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Got error [%ld] posting to web server\n",httpRes); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Got error [%ld] posting to web server [%s]\n",httpRes, globals.url);
} }
/* make sure we dont sleep on the last try */ /* make sure we dont sleep on the last try */
@ -151,9 +157,7 @@ static switch_status_t my_on_hangup(switch_core_session_t *session)
curTry=globals.retries; curTry=globals.retries;
} }
} }
} }
free(curl_xml_text);
curl_easy_cleanup(curl_handle); curl_easy_cleanup(curl_handle);
/* if we are here the web post failed for some reason */ /* if we are here the web post failed for some reason */
@ -174,16 +178,27 @@ static switch_status_t my_on_hangup(switch_core_session_t *session)
#endif #endif
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error![%s]\n", ebuf); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error![%s]\n", ebuf);
} }
free(path);
} }
} }
free(xml_text);
switch_xml_free(cdr);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n");
} }
return SWITCH_STATUS_SUCCESS;
success:
status = SWITCH_STATUS_SUCCESS;
error:
if (curl_handle) {
curl_easy_cleanup(curl_handle);
}
switch_safe_free(curl_xml_text);
switch_safe_free(xml_text_escaped);
switch_safe_free(path);
switch_safe_free(xml_text);
switch_xml_free(cdr);
return status;
} }
@ -221,9 +236,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_xml_cdr_load)
char *val = (char *) switch_xml_attr_soft(param, "value"); char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcasecmp(var, "cred")) { if (!strcasecmp(var, "cred")) {
globals.cred = val; globals.cred = strdup(val);
} else if (!strcasecmp(var, "url")) { } else if (!strcasecmp(var, "url")) {
globals.url = val; globals.url = strdup(val);
} else if (!strcasecmp(var, "delay")) { } else if (!strcasecmp(var, "delay")) {
globals.delay = (uint32_t) atoi(val); globals.delay = (uint32_t) atoi(val);
} else if (!strcasecmp(var, "retries")) { } else if (!strcasecmp(var, "retries")) {
@ -232,10 +247,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_xml_cdr_load)
if (switch_strlen_zero(val)) { if (switch_strlen_zero(val)) {
globals.logDir = SWITCH_GLOBAL_dirs.log_dir; globals.logDir = SWITCH_GLOBAL_dirs.log_dir;
} else { } else {
globals.logDir = val; globals.logDir = strdup(val);
} }
} else if (!strcasecmp(var, "errLogDir")) { } else if (!strcasecmp(var, "errLogDir")) {
globals.errLogDir = val; globals.errLogDir = strdup(val);
} }
} }