From 206626306493fde64e53c90859d18a1f2dad7b22 Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Tue, 25 Sep 2007 01:55:05 +0000 Subject: [PATCH] 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 --- src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c | 73 ++++++++++++++--------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c b/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c index c218f8d046..d9f1a184e8 100644 --- a/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c +++ b/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c @@ -59,23 +59,25 @@ static void httpCallBack() static switch_status_t my_on_hangup(switch_core_session_t *session) { switch_xml_t cdr; - char *xml_text; - char *path; + char *xml_text = NULL; + char *path = NULL; + char *curl_xml_text = NULL; + char *logdir = NULL; + char *xml_text_escaped = NULL; int fd = -1; uint32_t curTry; long httpRes; CURL *curl_handle = NULL; - char *curl_xml_text; - char *logdir; switch_channel_t *channel = switch_core_session_get_channel(session); uint32_t i; + switch_status_t status = SWITCH_STATUS_FALSE; if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) { /* build the XML */ if(!(xml_text = switch_mprintf("\n%s",switch_xml_toxml(cdr)) )) { 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? */ @@ -101,17 +103,24 @@ static switch_status_t my_on_hangup(switch_core_session_t *session) #endif 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 */ - if(!switch_strlen_zero(globals.url)) { + if (!switch_strlen_zero(globals.url)) { 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"); - free(xml_text); - return SWITCH_STATUS_FALSE; + goto error; + } + + 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)) { @@ -130,30 +139,25 @@ static switch_status_t my_on_hangup(switch_core_session_t *session) curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); // 302 recursion level */ - for(curTry=0;curTry<=globals.retries;curTry++) { + for (curTry=0;curTry<=globals.retries;curTry++) { curl_easy_perform(curl_handle); curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,&httpRes); - if(httpRes==200) { - curl_easy_cleanup(curl_handle); - free(curl_xml_text); - free(xml_text); - return SWITCH_STATUS_SUCCESS; + if (httpRes==200) { + goto success; } 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 */ - for(i=0;i