From: Rafael Marinheiro Date: Fri, 8 Jun 2018 13:24:20 +0000 (-0400) Subject: text protocol: Handle errors in set_option. X-Git-Tag: collectd-5.11.0~20^2~6 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=9f0d72fd36d4fb493a4ffc90a546d08962b3b773;p=thirdparty%2Fcollectd.git text protocol: Handle errors in set_option. --- diff --git a/src/utils/cmds/putval.c b/src/utils/cmds/putval.c index 86368837d..8611188b7 100644 --- a/src/utils/cmds/putval.c +++ b/src/utils/cmds/putval.c @@ -40,11 +40,11 @@ static int is_quoted(const char *str, size_t len) { return 0; } - return (str[0] == '"' && str[len - 1] == '"') || - (str[0] == '\'' && str[len - 1] == '\''); + return (str[0] == '"' && str[len - 1] == '"'); } -static int set_option(value_list_t *vl, const char *key, const char *value) { +static int set_option(value_list_t *vl, const char *key, const char *value, + cmd_error_handler_t *err) { if ((vl == NULL) || (key == NULL) || (value == NULL)) return -1; @@ -65,23 +65,30 @@ static int set_option(value_list_t *vl, const char *key, const char *value) { if (vl->meta == NULL) { vl->meta = meta_data_create(); if (vl->meta == NULL) { - return 1; + return CMD_ERROR; } } if (is_quoted(value, value_len)) { + int metadata_err; const char *value_str = strndup(value + 1, value_len - 2); if (value_str == NULL) { - return 1; + return CMD_ERROR; } - meta_data_add_string(vl->meta, meta_key, value_str); + metadata_err = meta_data_add_string(vl->meta, meta_key, value_str); free((void *)value_str); + if (metadata_err != 0) { + return CMD_ERROR; + } + return CMD_OK; } - return 1; + + cmd_error(CMD_NO_OPTION, err, "Non-string metadata not supported yet"); + return CMD_NO_OPTION; } else { - return 1; + return CMD_ERROR; } - return 0; + return CMD_OK; } /* int set_option */ /* @@ -185,10 +192,15 @@ cmd_status_t cmd_parse_putval(size_t argc, char **argv, status = cmd_parse_option(argv[i], &key, &value, err); if (status == CMD_OK) { + int option_err; + assert(key != NULL); assert(value != NULL); - set_option(&vl, key, value); - continue; + option_err = set_option(&vl, key, value, err); + if (option_err != CMD_OK && option_err != CMD_NO_OPTION) { + result = option_err; + break; + } } else if (status != CMD_NO_OPTION) { /* parse_option failed, buffer has been modified. * => we need to abort */