[collectd] [PATCH] utils_cmd_{get, put}val.c: Fixed usage of parse_identifier().

Sebastian Harl sh at tokkee.org
Tue Mar 25 15:58:15 CET 2008


This function modifies its first argument which, in these cases, is used
again after the function call. Now, a copy of the string is passed to
parse_identifier().

Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 src/utils_cmd_getval.c |   13 ++++++++++++-
 src/utils_cmd_putval.c |   18 ++++++++++++++++--
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c
index 85028d0..f110196 100644
--- a/src/utils_cmd_getval.c
+++ b/src/utils_cmd_getval.c
@@ -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;
@@ -57,7 +59,11 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
     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);
   }
 
@@ -84,6 +92,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num)
   {
     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 */
diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c
index ec0c09a..98b5043 100644
--- a/src/utils_cmd_putval.c
+++ b/src/utils_cmd_putval.c
@@ -121,6 +121,8 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 	int   status;
 	int   i;
 
+	char *identifier_copy;
+
 	const data_set_t *ds;
 	value_list_t vl = VALUE_LIST_INIT;
 
@@ -135,7 +137,11 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 		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)
@@ -143,6 +149,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 		DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
 		fprintf (fh, "-1 Cannot parse identifier.\n");
 		fflush (fh);
+		sfree (identifier_copy);
 		return (-1);
 	}
 
@@ -154,6 +161,8 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 				&& (strlen (type_instance) >= sizeof (vl.type_instance))))
 	{
 		fprintf (fh, "-1 Identifier too long.\n");
+		fflush (fh);
+		sfree (identifier_copy);
 		return (-1);
 	}
 
@@ -165,14 +174,18 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 		strcpy (vl.type_instance, type_instance);
 
 	ds = plugin_get_ds (type);
-	if (ds == NULL)
+	if (ds == NULL) {
+		sfree (identifier_copy);
 		return (-1);
+	}
 
 	vl.values_len = ds->ds_num;
 	vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
 	if (vl.values == NULL)
 	{
 		fprintf (fh, "-1 malloc failed.\n");
+		fflush (fh);
+		sfree (identifier_copy);
 		return (-1);
 	}
 
@@ -211,6 +224,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 	fflush (fh);
 
 	sfree (vl.values); 
+	sfree (identifier_copy);
 
 	return (0);
 } /* int handle_putval */
-- 
1.5.5.rc1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20080325/083a8f8b/attachment.pgp 


More information about the collectd mailing list