Pārlūkot izejas kodu

modules/utils: http_query fixes

- Credits to Guillaume Bour.
Juha Heinanen 11 gadi atpakaļ
vecāks
revīzija
e667cf0f35
1 mainītis faili ar 8 papildinājumiem un 4 dzēšanām
  1. 8 4
      modules/utils/functions.c

+ 8 - 4
modules/utils/functions.c

@@ -51,7 +51,7 @@ size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream)
     /* Allocate memory and copy */
     char* data;
 
-    data = (char*)malloc((size* nmemb) + 1);
+    data = (char*)pkg_malloc((size* nmemb) + 1);
     if (data == NULL) {
 	LM_ERR("cannot allocate memory for stream\n");
 	return CURLE_WRITE_ERROR;
@@ -76,7 +76,7 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst)
     CURLcode res;  
     str value;
     char *url, *at;
-    char* stream;
+    char* stream = NULL;
     long stat;
     pv_spec_t *dst;
     pv_value_t val;
@@ -112,10 +112,12 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst)
 
     res = curl_easy_perform(curl);  
     pkg_free(url);
-    curl_easy_cleanup(curl);
 
     if (res != CURLE_OK) {
 	LM_ERR("failed to perform curl\n");
+	curl_easy_cleanup(curl);
+	if(stream)
+		pkg_free(stream);
 	return -1;
     }
 
@@ -131,11 +133,13 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst)
 	}
 	val.rs.s = stream;
 	val.rs.len = at - stream;
-	LM_DBG("http)query result: %.*s\n", val.rs.len, val.rs.s);
+	LM_DBG("http_query result: %.*s\n", val.rs.len, val.rs.s);
 	val.flags = PV_VAL_STR;
 	dst = (pv_spec_t *)_dst;
 	dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
     }
 	
+    curl_easy_cleanup(curl);
+    pkg_free(stream);
     return stat;
 }