Skip to content

Commit 7e2a887

Browse files
committed
router: do logic matching using tag length only
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 0fdb8da commit 7e2a887

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

src/flb_router.c

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <fluent-bit/flb_info.h>
2121
#include <fluent-bit/flb_mem.h>
2222
#include <fluent-bit/flb_str.h>
23-
#include <fluent-bit/flb_sds.h>
2423
#include <fluent-bit/flb_input.h>
2524
#include <fluent-bit/flb_input_chunk.h>
2625
#include <fluent-bit/flb_output.h>
@@ -34,13 +33,33 @@
3433
#include <string.h>
3534

3635
/* wildcard support */
37-
/* tag and match should be null terminated. */
36+
/* match should be null terminated; tag length is provided explicitly. */
3837
static inline int router_match(const char *tag, int tag_len,
3938
const char *match,
4039
void *match_r)
4140
{
4241
int ret = FLB_FALSE;
43-
char *pos = NULL;
42+
const char *tag_end;
43+
const char *tag_cursor;
44+
size_t remaining;
45+
static const char empty_tag[] = "";
46+
47+
if (tag_len < 0) {
48+
return FLB_FALSE;
49+
}
50+
51+
if (!tag) {
52+
if (tag_len == 0) {
53+
tag = empty_tag;
54+
}
55+
else {
56+
return FLB_FALSE;
57+
}
58+
}
59+
60+
tag_end = tag + tag_len;
61+
tag_cursor = tag;
62+
remaining = (size_t) tag_len;
4463

4564
#ifdef FLB_HAVE_REGEX
4665
struct flb_regex *match_regex = match_r;
@@ -59,7 +78,11 @@ static inline int router_match(const char *tag, int tag_len,
5978
(void) match_r;
6079
#endif
6180

62-
while (match) {
81+
if (!match) {
82+
return ret;
83+
}
84+
85+
while (*match) {
6386
if (*match == '*') {
6487
while (*++match == '*'){
6588
/* skip successive '*' */
@@ -70,58 +93,53 @@ static inline int router_match(const char *tag, int tag_len,
7093
break;
7194
}
7295

73-
while ((pos = strchr(tag, (int) *match))) {
96+
const char *search = tag_cursor;
97+
98+
while (search < tag_end) {
99+
size_t span = (size_t) (tag_end - search);
100+
const char *pos;
101+
102+
pos = memchr(search, (unsigned char) *match, span);
103+
if (!pos) {
104+
break;
105+
}
106+
74107
#ifndef FLB_HAVE_REGEX
75-
if (router_match(pos, tag_len, match, NULL)) {
108+
if (router_match(pos, (int) (tag_end - pos), match, NULL)) {
76109
#else
77110
/* We don't need to pass the regex recursively,
78111
* we matched in order above
79112
*/
80-
if (router_match(pos, tag_len, match, NULL)) {
113+
if (router_match(pos, (int) (tag_end - pos), match, NULL)) {
81114
#endif
82115
ret = 1;
83-
break;
116+
goto done;
84117
}
85-
tag = pos+1;
118+
search = pos + 1;
86119
}
87120
break;
88121
}
89-
else if (*tag != *match) {
122+
else if (remaining == 0 || *tag_cursor != *match) {
90123
/* mismatch! */
91124
break;
92125
}
93-
else if (*tag == '\0') {
94-
/* end of tag. so matched! */
95-
ret = 1;
96-
break;
97-
}
98-
tag++;
126+
tag_cursor++;
127+
remaining--;
99128
match++;
100129
}
101130

131+
if (*match == '\0' && remaining == 0) {
132+
ret = 1;
133+
}
134+
135+
done:
102136
return ret;
103137
}
104138

105139
int flb_router_match(const char *tag, int tag_len, const char *match,
106140
void *match_regex)
107141
{
108-
int ret;
109-
flb_sds_t t;
110-
111-
if (tag[tag_len] != '\0') {
112-
t = flb_sds_create_len(tag, tag_len);
113-
if (!t) {
114-
return FLB_FALSE;
115-
}
116-
117-
ret = router_match(t, tag_len, match, match_regex);
118-
flb_sds_destroy(t);
119-
}
120-
else {
121-
ret = router_match(tag, tag_len, match, match_regex);
122-
}
123-
124-
return ret;
142+
return router_match(tag, tag_len, match, match_regex);
125143
}
126144

127145
/* Associate and input and output instances due to a previous match */

0 commit comments

Comments
 (0)