Replace all occurrences of `strcpy' with `sstrncpy'.
[collectd.git] / src / apache.c
index 280c687..54a5d56 100644 (file)
@@ -36,10 +36,10 @@ static char *cacert = NULL;
 
 static CURL *curl = NULL;
 
-#define ABUFFER_SIZE 16384
-static char apache_buffer[ABUFFER_SIZE];
-static int  apache_buffer_len = 0;
-static char apache_curl_error[CURL_ERROR_SIZE];
+static char  *apache_buffer = NULL;
+static size_t apache_buffer_size = 0;
+static size_t apache_buffer_fill = 0;
+static char   apache_curl_error[CURL_ERROR_SIZE];
 
 static const char *config_keys[] =
 {
@@ -50,21 +50,32 @@ static const char *config_keys[] =
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
+static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
+               void *stream)
 {
        size_t len = size * nmemb;
 
-       if ((apache_buffer_len + len) >= ABUFFER_SIZE)
-       {
-               len = (ABUFFER_SIZE - 1) - apache_buffer_len;
-       }
-
        if (len <= 0)
                return (len);
 
-       memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
-       apache_buffer_len += len;
-       apache_buffer[apache_buffer_len] = '\0';
+       if ((apache_buffer_fill + len) >= apache_buffer_size)
+       {
+               char *temp;
+
+               temp = (char *) realloc (apache_buffer,
+                               apache_buffer_fill + len + 1);
+               if (temp == NULL)
+               {
+                       ERROR ("apache plugin: realloc failed.");
+                       return (0);
+               }
+               apache_buffer = temp;
+               apache_buffer_size = apache_buffer_fill + len + 1;
+       }
+
+       memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
+       apache_buffer_fill += len;
+       apache_buffer[apache_buffer_fill] = 0;
 
        return (len);
 }
@@ -162,9 +173,9 @@ static void submit_counter (const char *type, const char *type_instance,
        vl.values = values;
        vl.values_len = 1;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "apache");
-       strcpy (vl.plugin_instance, "");
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
 
        if (type_instance != NULL)
        {
@@ -187,9 +198,9 @@ static void submit_gauge (const char *type, const char *type_instance,
        vl.values = values;
        vl.values_len = 1;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "apache");
-       strcpy (vl.plugin_instance, "");
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
 
        if (type_instance != NULL)
        {
@@ -269,7 +280,7 @@ static int apache_read (void)
        if (url == NULL)
                return (-1);
 
-       apache_buffer_len = 0;
+       apache_buffer_fill = 0;
        if (curl_easy_perform (curl) != 0)
        {
                ERROR ("apache: curl_easy_perform failed: %s",
@@ -312,7 +323,7 @@ static int apache_read (void)
                }
        }
 
-       apache_buffer_len = 0;
+       apache_buffer_fill = 0;
 
        return (0);
 } /* int apache_read */