fix potential curl plugin curl_slist_append memory leak
[collectd.git] / src / curl.c
index 280e61c..8b30fa4 100644 (file)
@@ -165,16 +165,19 @@ static void cc_web_page_free (web_page_t *wp) /* {{{ */
 static int cc_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
     oconfig_item_t *ci)
 {
+  struct curl_slist *temp = NULL;
   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
   {
     WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
     return (-1);
   }
 
-  *dest = curl_slist_append(*dest, ci->values[0].value.string);
-  if (*dest == NULL)
+  temp = curl_slist_append(*dest, ci->values[0].value.string);
+  if (temp == NULL)
     return (-1);
 
+  *dest = temp;
+
   return (0);
 } /* }}} int cc_config_append_string */
 
@@ -325,7 +328,10 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */
   } /* while (status == 0) */
 
   if (status != 0)
+  {
+    cc_web_match_free (match);
     return (status);
+  }
 
   match->match = match_create_simple (match->regex, match->exclude_regex,
       match->dstype);
@@ -569,6 +575,7 @@ static int cc_init (void) /* {{{ */
     INFO ("curl plugin: No pages have been defined.");
     return (-1);
   }
+  curl_global_init (CURL_GLOBAL_SSL);
   return (0);
 } /* }}} int cc_init */
 
@@ -586,7 +593,8 @@ static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
   sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
   sstrncpy (vl.type, wm->type, sizeof (vl.type));
-  sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
+  if (wm->instance != NULL)
+    sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
 
   plugin_dispatch_values (&vl);
 } /* }}} void cc_submit */
@@ -639,7 +647,7 @@ static int cc_read_page (web_page_t *wp) /* {{{ */
   status = curl_easy_perform (wp->curl);
   if (status != CURLE_OK)
   {
-    ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
+    ERROR ("curl plugin: curl_easy_perform failed with status %i: %s",
         status, wp->curl_errbuf);
     return (-1);
   }
@@ -652,7 +660,7 @@ static int cc_read_page (web_page_t *wp) /* {{{ */
     long response_code = 0;
     status = curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
     if(status != CURLE_OK) {
-      ERROR ("curl plugin: Fetching response code failed with staus %i: %s",
+      ERROR ("curl plugin: Fetching response code failed with status %i: %s",
         status, wp->curl_errbuf);
     } else {
       cc_submit_response_code(wp, response_code);
@@ -678,6 +686,7 @@ static int cc_read_page (web_page_t *wp) /* {{{ */
     }
 
     cc_submit (wp, wm, mv);
+    match_value_reset (mv);
   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
 
   return (0);