Skip to content

Commit

Permalink
compat: decode_statsd: Preserve the default argument when dividing wi…
Browse files Browse the repository at this point in the history
…th a delimiter

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Jul 30, 2024
1 parent d738310 commit 7fa2414
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
41 changes: 33 additions & 8 deletions include/cmetrics/cmt_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,45 @@
#ifdef _WIN32
#include <windows.h>
#endif
#include <string.h>

/* This function is copied from monkey/monkey.
https://github.com/monkey/monkey/blob/2567a70912ed7a68d9e75dca3cf22d3927fea99a/mk_core/deps/libevent/evdns.c#L3323 */
/* This function is based on monkey/monkey.
* https://github.com/monkey/monkey/blob/2567a70912ed7a68d9e75dca3cf22d3927fea99a/mk_core/deps/libevent/evdns.c#L3323.
* Also preserving the original char pointer and allocation would be occurred.
*/
static inline char *
cmt_platform_strtok_r(char *s, const char *delim, char **state) {
char *cp, *start;
start = cp = s ? s : *state;
if (!cp)
cmt_platform_strtok_s(char *s, const char *delim, char **state, char **allocated)
{
char *cp = NULL, *start = NULL, *tmp = NULL;
size_t len = 0;

if (s != NULL) {
len = strlen(s);
tmp = calloc(1, sizeof(char) * len + 1);
if (tmp == NULL) {
return NULL;
}
strncpy(tmp, s, len);
start = cp = tmp;
}
else {
start = cp = *state;
}

if (tmp != NULL) {
*allocated = tmp;
}

if (!cp) {
return NULL;
while (*cp && !strchr(delim, *cp))
}
while (*cp && !strchr(delim, *cp)) {
++cp;
}
if (!*cp) {
if (cp == start)
if (cp == start) {
return NULL;
}
*state = NULL;
return start;
} else {
Expand Down
22 changes: 17 additions & 5 deletions src/cmt_decode_statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static int decode_labels(struct cmt *cmt,
cfl_sds_t label_k, label_v, tmp;
int result;
char *store;
char *alloced = NULL;

result = CMT_DECODE_STATSD_SUCCESS;

Expand Down Expand Up @@ -139,7 +140,7 @@ static int decode_labels(struct cmt *cmt,
}

if (labels != NULL) {
label_kv = cmt_platform_strtok_r(labels, ",", &store);
label_kv = cmt_platform_strtok_s(labels, ",", &store, &alloced);

while (label_kv != NULL) {
colon = strchr(label_kv, ':');
Expand Down Expand Up @@ -188,9 +189,8 @@ static int decode_labels(struct cmt *cmt,
}

cfl_sds_destroy(label_k);

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

Expand All @@ -215,6 +215,11 @@ static int decode_labels(struct cmt *cmt,
cfl_sds_destroy(label_v);
}

if (alloced != NULL) {
free(alloced);
alloced = NULL;
}

free(value_index_list);

return result;
Expand Down Expand Up @@ -528,8 +533,9 @@ static int decode_metrics_lines(struct cmt *cmt,
int ret = CMT_DECODE_STATSD_SUCCESS;
char *line = NULL;
char *store = NULL;
char *alloced = NULL;

line = cmt_platform_strtok_r(in_buf, "\n", &store);
line = cmt_platform_strtok_s(in_buf, "\n", &store, &alloced);
while (line != NULL) {
/* StatsD format always has | at least one. */
if (strstr(line, "|") == NULL) {
Expand All @@ -542,8 +548,14 @@ static int decode_metrics_lines(struct cmt *cmt,

break;
}

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

if (alloced != NULL) {
free(alloced);
alloced = NULL;
}

return ret;
Expand Down

0 comments on commit 7fa2414

Please sign in to comment.