Code

rrdtool plugin: Improve handling of the "DataDir" option.
authorFlorian Forster <octo@collectd.org>
Tue, 8 Dec 2015 13:22:54 +0000 (14:22 +0100)
committerFlorian Forster <octo@collectd.org>
Tue, 8 Dec 2015 13:22:54 +0000 (14:22 +0100)
CID: 37977

src/rrdtool.c

index 9b5723ad2992c4d3cdcaa448c9462afead768866..9b04d10ef753a8cf4818587dda4be1ebc340ad59 100644 (file)
@@ -1008,23 +1008,32 @@ static int rrd_config (const char *key, const char *value)
        }
        else if (strcasecmp ("DataDir", key) == 0)
        {
-               if (datadir != NULL)
-                       free (datadir);
-               datadir = strdup (value);
-               if (datadir != NULL)
+               char *tmp;
+               size_t len;
+
+               tmp = strdup (value);
+               if (tmp == NULL)
                {
-                       int len = strlen (datadir);
-                       while ((len > 0) && (datadir[len - 1] == '/'))
-                       {
-                               len--;
-                               datadir[len] = '\0';
-                       }
-                       if (len <= 0)
-                       {
-                               free (datadir);
-                               datadir = NULL;
-                       }
+                       ERROR ("rrdtool plugin: strdup failed.");
+                       return (1);
+               }
+
+               len = strlen (datadir);
+               while ((len > 0) && (datadir[len - 1] == '/'))
+               {
+                       len--;
+                       datadir[len] = 0;
                }
+
+               if (len == 0)
+               {
+                       ERROR ("rrdtool plugin: Invalid \"DataDir\" option.");
+                       sfree (tmp);
+                       return (1);
+               }
+
+               sfree (datadir);
+               datadir = tmp;
        }
        else if (strcasecmp ("StepSize", key) == 0)
        {