Skip to content

Commit

Permalink
decoder_stastd: Use cfl_utils_split function to avoid strtok
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Aug 6, 2024
1 parent 3616540 commit 1f9b002
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/cmt_decode_statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ static int decode_labels(struct cmt *cmt,
char *label_kv, *colon;
cfl_sds_t label_k, label_v, tmp;
int result;
char *store;
struct cfl_list *head = NULL;
struct cfl_list *kvs = NULL;
struct cfl_split_entry *cur = NULL;

result = CMT_DECODE_STATSD_SUCCESS;

Expand Down Expand Up @@ -139,22 +141,36 @@ static int decode_labels(struct cmt *cmt,
}

if (labels != NULL) {
label_kv = cmt_platform_strtok_r(labels, ",", &store);
kvs = cfl_utils_split(labels, ',', -1 );
if (kvs == NULL) {
goto split_error;
}

cfl_list_foreach(head, kvs) {
retry:
cur = cfl_list_entry(head, struct cfl_split_entry, _head);
label_kv = cur->value;

while (label_kv != NULL) {
colon = strchr(label_kv, ':');
if (colon == NULL) {
goto retry;
}
label_k = cfl_sds_create_len(label_kv, colon - label_kv);
if (label_k == NULL) {
free(value_index_list);
if (kvs != NULL) {
cfl_utils_split_free(kvs);
}

return CMT_DECODE_STATSD_INVALID_TAG_FORMAT_ERROR;
}
label_v = cfl_sds_create_len(colon + 1, strlen(label_kv) - strlen(label_k) - 1);
if (label_v == NULL) {
cfl_sds_destroy(label_k);
free(value_index_list);
if (kvs != NULL) {
cfl_utils_split_free(kvs);
}

return CMT_DECODE_STATSD_INVALID_TAG_FORMAT_ERROR;
}
Expand All @@ -175,6 +191,9 @@ static int decode_labels(struct cmt *cmt,
cfl_sds_destroy(label_k);
cfl_sds_destroy(label_v);
free(value_index_list);
if (kvs != NULL) {
cfl_utils_split_free(kvs);
}

return CMT_DECODE_STATSD_INVALID_ARGUMENT_ERROR;
}
Expand All @@ -188,12 +207,11 @@ static int decode_labels(struct cmt *cmt,
}

cfl_sds_destroy(label_k);

retry:
label_kv = cmt_platform_strtok_r(NULL, ",", &store);
}
}

split_error: /* Nop for adding labels */

map_label_count = cfl_list_size(&map->label_keys);

for (map_label_index = 0 ;
Expand All @@ -215,6 +233,10 @@ static int decode_labels(struct cmt *cmt,
cfl_sds_destroy(label_v);
}

if (kvs != NULL) {
cfl_utils_split_free(kvs);
}

free(value_index_list);

return result;
Expand Down Expand Up @@ -526,27 +548,39 @@ static int decode_metrics_lines(struct cmt *cmt,
int flags)
{
int ret = CMT_DECODE_STATSD_SUCCESS;
char *line = NULL;
char *store = NULL;
struct cfl_list *head = NULL;
struct cfl_list *kvs = NULL;
struct cfl_split_entry *cur = NULL;

line = cmt_platform_strtok_r(in_buf, "\n", &store);
while (line != NULL) {
kvs = cfl_utils_split(in_buf, '\n', -1 );
if (kvs == NULL) {
goto split_error;
}

cfl_list_foreach(head, kvs) {
retry:
cur = cfl_list_entry(head, struct cfl_split_entry, _head);
/* StatsD format always has | at least one. */
if (strstr(line, "|") == NULL) {
if (strstr(cur->value, "|") == NULL) {
goto retry;
}

ret = statsd_process_line(cmt, line, flags);
ret = statsd_process_line(cmt, cur->value, flags);
if (ret != CMT_DECODE_STATSD_SUCCESS) {
ret = CMT_DECODE_STATSD_DECODE_ERROR;

break;
}
retry:
line = cmt_platform_strtok_r(NULL, "\n", &store);
}

if (kvs != NULL) {
cfl_utils_split_free(kvs);
}

return ret;

split_error:
return -1;
}

int cmt_decode_statsd_create(struct cmt **out_cmt, char *in_buf, size_t in_size, int flags)
Expand Down

0 comments on commit 1f9b002

Please sign in to comment.