Merge branch 'collectd-4.3'
[collectd.git] / src / utils_cmd_getval.c
index fa5cdf2..f110196 100644 (file)
@@ -35,6 +35,8 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
   gauge_t *values;
   size_t values_num;
 
+  char *identifier_copy;
+
   const data_set_t *ds;
 
   int   status;
@@ -52,12 +54,16 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
 
   if (strlen (fields[1]) < strlen ("h/p/t"))
   {
-    fprintf (fh, "-1 Invalied identifier, %s", fields[1]);
+    fprintf (fh, "-1 Invalied identifier, %s\n", fields[1]);
     fflush (fh);
     return (-1);
   }
 
-  status = parse_identifier (fields[1], &hostname,
+  /* parse_identifier() modifies its first argument,
+   * returning pointers into it */
+  identifier_copy = sstrdup (fields[1]);
+
+  status = parse_identifier (identifier_copy, &hostname,
       &plugin, &plugin_instance,
       &type, &type_instance);
   if (status != 0)
@@ -65,6 +71,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
     DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
     fprintf (fh, "-1 Cannot parse identifier.\n");
     fflush (fh);
+    sfree (identifier_copy);
     return (-1);
   }
 
@@ -74,6 +81,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
     DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
     fprintf (fh, "-1 Type `%s' is unknown.\n", type);
     fflush (fh);
+    sfree (identifier_copy);
     return (-1);
   }
 
@@ -82,8 +90,9 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
   status = uc_get_rate_by_name (fields[1], &values, &values_num);
   if (status != 0)
   {
-    fprintf (fh, "-1 No such value");
+    fprintf (fh, "-1 No such value\n");
     fflush (fh);
+    sfree (identifier_copy);
     return (-1);
   }
 
@@ -95,6 +104,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
     fprintf (fh, "-1 Error reading value from cache.\n");
     fflush (fh);
     sfree (values);
+    sfree (identifier_copy);
     return (-1);
   }
 
@@ -111,6 +121,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
   fflush (fh);
 
   sfree (values);
+  sfree (identifier_copy);
 
   return (0);
 } /* int handle_getval */