From: Rafael Marinheiro Date: Mon, 21 May 2018 15:49:39 +0000 (-0400) Subject: text protocol: Require quotes for string metadata. X-Git-Tag: collectd-5.11.0~20^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd4c8bc9ed85cf1dfbbfc4be17a9522948600b40;p=thirdparty%2Fcollectd.git text protocol: Require quotes for string metadata. This changes the metadata option to require quotes for string values. This also adds tests for the new option. I also ran clang-format on utils_cmds_test.c. --- diff --git a/src/utils/cmds/cmds_test.c b/src/utils/cmds/cmds_test.c index edbf5c95e..9e7882031 100644 --- a/src/utils/cmds/cmds_test.c +++ b/src/utils/cmds/cmds_test.c @@ -204,6 +204,18 @@ static struct { CMD_OK, CMD_PUTVAL, }, + { + "PUTVAL myhost/magic/MAGIC meta:KEY=\"string_value\" 1234:42", + NULL, + CMD_OK, + CMD_PUTVAL, + }, + { + "PUTVAL myhost/magic/MAGIC meta:KEY='string_value' 1234:42", + NULL, + CMD_OK, + CMD_PUTVAL, + }, /* Invalid PUTVAL commands. */ { diff --git a/src/utils/cmds/putval.c b/src/utils/cmds/putval.c index 11f0fcb61..77debe352 100644 --- a/src/utils/cmds/putval.c +++ b/src/utils/cmds/putval.c @@ -35,6 +35,15 @@ * private helper functions */ +static int is_quoted(const char *str, size_t len) { + if (len < 2) { + return 0; + } + + return (str[0] == '"' && str[len - 1] == '"') || + (str[0] == '\'' && str[len - 1] == '\''); +} + static int set_option(value_list_t *vl, const char *key, const char *value) { if ((vl == NULL) || (key == NULL) || (value == NULL)) return -1; @@ -50,13 +59,21 @@ static int set_option(value_list_t *vl, const char *key, const char *value) { if ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0)) vl->interval = DOUBLE_TO_CDTIME_T(tmp); } else if (strncasecmp("meta:", key, 5) == 0) { + const char *meta_key = key + 5; + size_t value_len = strlen(value); + if (vl->meta == NULL) { vl->meta = meta_data_create(); if (vl->meta == NULL) { return 1; } } - return meta_data_add_string(vl->meta, key + 5, value); + + if (is_quoted(value, value_len)) { + const char *value_str = strndup(value + 1, value_len - 2); + return meta_data_add_string(vl->meta, meta_key, value_str); + } + return 1; } else { return 1; }