From 921d36e4b820ee1539f0da22c824e2739f1e546f Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 16 Mar 2012 18:26:08 +0100 Subject: [PATCH] src/common.c: parse_value: Strip trailing whitespace before parsing. This hopefully avoids the annoying "trailing garbage" message when the buffer only contains a newline or space at the end. --- src/common.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/common.c b/src/common.c index 530f7330..c5bd6470 100644 --- a/src/common.c +++ b/src/common.c @@ -849,9 +849,25 @@ int parse_identifier (char *str, char **ret_host, return (0); } /* int parse_identifier */ -int parse_value (const char *value, value_t *ret_value, int ds_type) +int parse_value (const char *value_orig, value_t *ret_value, int ds_type) { + char *value; char *endptr = NULL; + size_t value_len; + + if (value_orig == NULL) + return (EINVAL); + + value = strdup (value_orig); + if (value == NULL) + return (ENOMEM); + value_len = strlen (value); + + while ((value_len > 0) && isspace ((int) value[value_len - 1])) + { + value[value_len - 1] = 0; + value_len--; + } switch (ds_type) { @@ -872,11 +888,13 @@ int parse_value (const char *value, value_t *ret_value, int ds_type) break; default: + sfree (value); ERROR ("parse_value: Invalid data source type: %i.", ds_type); return -1; } if (value == endptr) { + sfree (value); ERROR ("parse_value: Failed to parse string as %s: %s.", DS_TYPE_TO_STRING (ds_type), value); return -1; @@ -884,8 +902,9 @@ int parse_value (const char *value, value_t *ret_value, int ds_type) else if ((NULL != endptr) && ('\0' != *endptr)) INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. " "Input string was \"%s\".", - endptr, DS_TYPE_TO_STRING (ds_type), value); + endptr, DS_TYPE_TO_STRING (ds_type), value_orig); + sfree (value); return 0; } /* int parse_value */ -- 2.30.2