projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Auto-Merge pull request #2507 from rpv-tomsk/issue-2506
[collectd.git]
/
src
/
utils_cmd_putval.c
diff --git
a/src/utils_cmd_putval.c
b/src/utils_cmd_putval.c
index
817f954
..
6f1bc39
100644
(file)
--- a/
src/utils_cmd_putval.c
+++ b/
src/utils_cmd_putval.c
@@
-37,7
+37,7
@@
static int set_option(value_list_t *vl, const char *key, const char *value) {
if ((vl == NULL) || (key == NULL) || (value == NULL))
static int set_option(value_list_t *vl, const char *key, const char *value) {
if ((vl == NULL) || (key == NULL) || (value == NULL))
- return
(-1)
;
+ return
-1
;
if (strcasecmp("interval", key) == 0) {
double tmp;
if (strcasecmp("interval", key) == 0) {
double tmp;
@@
-50,9
+50,9
@@
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 ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0))
vl->interval = DOUBLE_TO_CDTIME_T(tmp);
} else
- return
(1)
;
+ return
1
;
- return
(0)
;
+ return
0
;
} /* int set_option */
/*
} /* int set_option */
/*
@@
-81,12
+81,12
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
if ((ret_putval == NULL) || (opts == NULL)) {
errno = EINVAL;
cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
if ((ret_putval == NULL) || (opts == NULL)) {
errno = EINVAL;
cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
- return
(CMD_ERROR)
;
+ return
CMD_ERROR
;
}
if (argc < 2) {
cmd_error(CMD_PARSE_ERROR, err, "Missing identifier and/or value-list.");
}
if (argc < 2) {
cmd_error(CMD_PARSE_ERROR, err, "Missing identifier and/or value-list.");
- return
(CMD_PARSE_ERROR)
;
+ return
CMD_PARSE_ERROR
;
}
identifier = argv[0];
}
identifier = argv[0];
@@
-103,7
+103,7
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
identifier_copy);
sfree(identifier_copy);
cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
identifier_copy);
sfree(identifier_copy);
- return
(CMD_PARSE_ERROR)
;
+ return
CMD_PARSE_ERROR
;
}
if ((strlen(hostname) >= sizeof(vl.host)) ||
}
if ((strlen(hostname) >= sizeof(vl.host)) ||
@@
-114,7
+114,7
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
(strlen(type_instance) >= sizeof(vl.type_instance)))) {
cmd_error(CMD_PARSE_ERROR, err, "Identifier too long.");
sfree(identifier_copy);
(strlen(type_instance) >= sizeof(vl.type_instance)))) {
cmd_error(CMD_PARSE_ERROR, err, "Identifier too long.");
sfree(identifier_copy);
- return
(CMD_PARSE_ERROR)
;
+ return
CMD_PARSE_ERROR
;
}
sstrncpy(vl.host, hostname, sizeof(vl.host));
}
sstrncpy(vl.host, hostname, sizeof(vl.host));
@@
-129,7
+129,7
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
if (ds == NULL) {
cmd_error(CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
sfree(identifier_copy);
if (ds == NULL) {
cmd_error(CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
sfree(identifier_copy);
- return
(CMD_PARSE_ERROR)
;
+ return
CMD_PARSE_ERROR
;
}
hostname = NULL;
}
hostname = NULL;
@@
-138,20
+138,12
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
type = NULL;
type_instance = NULL;
type = NULL;
type_instance = NULL;
- vl.values_len = ds->ds_num;
- vl.values = malloc(vl.values_len * sizeof(*vl.values));
- if (vl.values == NULL) {
- cmd_error(CMD_ERROR, err, "malloc failed.");
- sfree(identifier_copy);
- return (CMD_ERROR);
- }
-
ret_putval->raw_identifier = identifier_copy;
if (ret_putval->raw_identifier == NULL) {
cmd_error(CMD_ERROR, err, "malloc failed.");
cmd_destroy_putval(ret_putval);
sfree(vl.values);
ret_putval->raw_identifier = identifier_copy;
if (ret_putval->raw_identifier == NULL) {
cmd_error(CMD_ERROR, err, "malloc failed.");
cmd_destroy_putval(ret_putval);
sfree(vl.values);
- return
(CMD_ERROR)
;
+ return
CMD_ERROR
;
}
/* All the remaining fields are part of the option list. */
}
/* All the remaining fields are part of the option list. */
@@
-177,35
+169,48
@@
cmd_status_t cmd_parse_putval(size_t argc, char **argv,
/* else: cmd_parse_option did not find an option; treat this as a
* value list. */
/* else: cmd_parse_option did not find an option; treat this as a
* value list. */
+ vl.values_len = ds->ds_num;
+ vl.values = calloc(vl.values_len, sizeof(*vl.values));
+ if (vl.values == NULL) {
+ cmd_error(CMD_ERROR, err, "malloc failed.");
+ result = CMD_ERROR;
+ break;
+ }
+
status = parse_values(argv[i], &vl, ds);
if (status != 0) {
cmd_error(CMD_PARSE_ERROR, err, "Parsing the values string failed.");
result = CMD_PARSE_ERROR;
status = parse_values(argv[i], &vl, ds);
if (status != 0) {
cmd_error(CMD_PARSE_ERROR, err, "Parsing the values string failed.");
result = CMD_PARSE_ERROR;
+ vl.values_len = 0;
+ sfree(vl.values);
break;
}
break;
}
- tmp =
(value_list_t *)realloc(ret_putval->vl, (ret_putval->vl_num + 1) *
-
sizeof(*ret_putval->vl));
+ tmp =
realloc(ret_putval->vl,
+
(ret_putval->vl_num + 1) *
sizeof(*ret_putval->vl));
if (tmp == NULL) {
cmd_error(CMD_ERROR, err, "realloc failed.");
cmd_destroy_putval(ret_putval);
result = CMD_ERROR;
if (tmp == NULL) {
cmd_error(CMD_ERROR, err, "realloc failed.");
cmd_destroy_putval(ret_putval);
result = CMD_ERROR;
+ vl.values_len = 0;
+ sfree(vl.values);
break;
}
ret_putval->vl = tmp;
ret_putval->vl_num++;
memcpy(&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof(vl));
break;
}
ret_putval->vl = tmp;
ret_putval->vl_num++;
memcpy(&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof(vl));
+
+ /* pointer is now owned by ret_putval->vl[] */
+ vl.values_len = 0;
+ vl.values = NULL;
} /* while (*buffer != 0) */
/* Done parsing the options. */
} /* while (*buffer != 0) */
/* Done parsing the options. */
- if (result != CMD_OK) {
- if (ret_putval->vl_num == 0)
- sfree(vl.values);
+ if (result != CMD_OK)
cmd_destroy_putval(ret_putval);
cmd_destroy_putval(ret_putval);
- }
- return
(result)
;
+ return
result
;
} /* cmd_status_t cmd_parse_putval */
void cmd_destroy_putval(cmd_putval_t *putval) {
} /* cmd_status_t cmd_parse_putval */
void cmd_destroy_putval(cmd_putval_t *putval) {
@@
-215,8
+220,7
@@
void cmd_destroy_putval(cmd_putval_t *putval) {
sfree(putval->raw_identifier);
for (size_t i = 0; i < putval->vl_num; ++i) {
sfree(putval->raw_identifier);
for (size_t i = 0; i < putval->vl_num; ++i) {
- if (i == 0) /* values is shared between all entries */
- sfree(putval->vl[i].values);
+ sfree(putval->vl[i].values);
meta_data_destroy(putval->vl[i].meta);
putval->vl[i].meta = NULL;
}
meta_data_destroy(putval->vl[i].meta);
putval->vl[i].meta = NULL;
}
@@
-235,12
+239,12
@@
cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
(void *)fh, buffer);
if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
(void *)fh, buffer);
if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
- return
(status)
;
+ return
status
;
if (cmd.type != CMD_PUTVAL) {
cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
CMD_TO_STRING(cmd.type));
cmd_destroy(&cmd);
if (cmd.type != CMD_PUTVAL) {
cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
CMD_TO_STRING(cmd.type));
cmd_destroy(&cmd);
- return
(CMD_UNKNOWN_COMMAND)
;
+ return
CMD_UNKNOWN_COMMAND
;
}
for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
}
for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
@@
-252,7
+256,7
@@
cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
(cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
cmd_destroy(&cmd);
(cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
cmd_destroy(&cmd);
- return
(CMD_OK)
;
+ return
CMD_OK
;
} /* int cmd_handle_putval */
int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
} /* int cmd_handle_putval */
int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
@@
-263,19
+267,19
@@
int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
status = FORMAT_VL(buffer_ident, sizeof(buffer_ident), vl);
if (status != 0)
status = FORMAT_VL(buffer_ident, sizeof(buffer_ident), vl);
if (status != 0)
- return
(status)
;
+ return
status
;
escape_string(buffer_ident, sizeof(buffer_ident));
status = format_values(buffer_values, sizeof(buffer_values), ds, vl,
/* store rates = */ 0);
if (status != 0)
escape_string(buffer_ident, sizeof(buffer_ident));
status = format_values(buffer_values, sizeof(buffer_values), ds, vl,
/* store rates = */ 0);
if (status != 0)
- return
(status)
;
+ return
status
;
escape_string(buffer_values, sizeof(buffer_values));
escape_string(buffer_values, sizeof(buffer_values));
- s
s
nprintf(ret, ret_len, "PUTVAL %s interval=%.3f %s", buffer_ident,
-
(vl->interval > 0) ? CDTIME_T_TO_DOUBLE(vl->interval)
-
: CDTIME_T_TO_DOUBLE(plugin_get_interval()),
-
buffer_values);
+ snprintf(ret, ret_len, "PUTVAL %s interval=%.3f %s", buffer_ident,
+ (vl->interval > 0) ? CDTIME_T_TO_DOUBLE(vl->interval)
+ : CDTIME_T_TO_DOUBLE(plugin_get_interval()),
+ buffer_values);
- return
(0)
;
+ return
0
;
} /* }}} int cmd_create_putval */
} /* }}} int cmd_create_putval */