Skip to content

Commit

Permalink
compat: decode_statsd: Import and use a generic safe version of strtok_r
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Jul 26, 2024
1 parent 0a1555e commit 7dae62a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 22 additions & 1 deletion include/cmetrics/cmt_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,30 @@
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#define strtok_r strtok_s
#endif

/* This function is copied from monkey/monkey.
https://github.com/monkey/monkey/blob/2567a70912ed7a68d9e75dca3cf22d3927fea99a/mk_core/deps/libevent/evdns.c#L3323 */
static inline char *
cmt_platform_strtok_r(char *s, const char *delim, char **state) {
char *cp, *start;
start = cp = s ? s : *state;
if (!cp)
return NULL;
while (*cp && !strchr(delim, *cp))
++cp;
if (!*cp) {
if (cp == start)
return NULL;
*state = NULL;
return start;
} else {
*cp++ = '\0';
*state = cp;
return start;
}
}

static inline struct tm *cmt_platform_gmtime_r(const time_t *timep, struct tm *result)
{
#ifdef CMT_HAVE_GMTIME_S
Expand Down
8 changes: 4 additions & 4 deletions src/cmt_decode_statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static int decode_labels(struct cmt *cmt,
}

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

while (label_kv != NULL) {
colon = strchr(label_kv, ':');
Expand Down Expand Up @@ -185,7 +185,7 @@ static int decode_labels(struct cmt *cmt,
cfl_sds_destroy(label_k);

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

Expand Down Expand Up @@ -524,7 +524,7 @@ static int decode_metrics_lines(struct cmt *cmt,
char *line = NULL;
char *store = NULL;

line = strtok_r(in_buf, "\n", &store);
line = cmt_platform_strtok_r(in_buf, "\n", &store);
while (line != NULL) {
/* StatsD format always has | at least one. */
if (strstr(line, "|") == NULL) {
Expand All @@ -538,7 +538,7 @@ static int decode_metrics_lines(struct cmt *cmt,
break;
}
retry:
line = strtok_r(NULL, "\n", &store);
line = cmt_platform_strtok_r(NULL, "\n", &store);
}

return ret;
Expand Down

0 comments on commit 7dae62a

Please sign in to comment.