Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/flb_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static int str_to_regex(const char *pattern, OnigRegex *reg)

option = check_option(start, end, &new_end);

if (pattern[0] == '/' && pattern[len - 1] == '/') {
if (len > 1 && pattern[0] == '/' && pattern[len - 1] == '/') {
start++;
end--;
}
Expand Down
92 changes: 92 additions & 0 deletions tests/internal/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,104 @@ static void test_option_i_x()
}
}

static void test_literal_slash()
{
struct flb_regex *regex = NULL;
int ret;

regex = flb_regex_create("/");
if (!TEST_CHECK(regex != NULL)) {
TEST_MSG("flb_regex_create failed");
exit(1);
}

ret = flb_regex_match(regex, (unsigned char *) "/tmp", 4);
if (!TEST_CHECK(ret == 1)) {
TEST_MSG("literal slash did not match");
flb_regex_destroy(regex);
exit(1);
}

ret = flb_regex_match(regex, (unsigned char *) "tmp", 3);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("literal slash matched string without slash");
flb_regex_destroy(regex);
exit(1);
}

ret = flb_regex_destroy(regex);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("flb_regex_destroy failed");
exit(1);
}
}

static void test_slash_delimited_pattern()
{
struct flb_regex *regex = NULL;
int ret;

regex = flb_regex_create("/tmp/");
if (!TEST_CHECK(regex != NULL)) {
TEST_MSG("flb_regex_create failed");
exit(1);
}

ret = flb_regex_match(regex, (unsigned char *) "tmp", 3);
if (!TEST_CHECK(ret == 1)) {
TEST_MSG("slash-delimited pattern did not match");
flb_regex_destroy(regex);
exit(1);
}

ret = flb_regex_destroy(regex);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("flb_regex_destroy failed");
exit(1);
}
}

static void test_slash_delimited_literal_slashes()
{
struct flb_regex *regex = NULL;
int ret;

regex = flb_regex_create("//tmp//");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so we have to use // to get one slash - should we have a different delimiter, e.g. \/?

Just worried we will end up with the nightmare of Java regexes and string escaping otherwise, e.g. if we want two slashes you have to have four, not three, etc.

@cosmo0920 cosmo0920 Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we have to use double slash in the beginning and the end of regex literals. Not intending on double slash anywhere on the regex literals for our regex engine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we do a docs PR btw @cosmo0920 ?

if (!TEST_CHECK(regex != NULL)) {
TEST_MSG("flb_regex_create failed");
exit(1);
}

ret = flb_regex_match(regex, (unsigned char *) "/tmp/", 5);
if (!TEST_CHECK(ret == 1)) {
TEST_MSG("slash-delimited literal slash pattern did not match");
flb_regex_destroy(regex);
exit(1);
}

ret = flb_regex_match(regex, (unsigned char *) "tmp", 3);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("slash-delimited literal slash pattern matched string without slashes");
flb_regex_destroy(regex);
exit(1);
}

ret = flb_regex_destroy(regex);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("flb_regex_destroy failed");
exit(1);
}
}

TEST_LIST = {
{ "basic" , test_basic},
{ "uri" , test_uri},
{ "option_ignore_case", test_option_ignore_case},
{ "option_multiline" , test_option_multiline},
{ "option_extend" , test_option_extend},
{ "option_i_x" , test_option_i_x},
{ "literal_slash" , test_literal_slash},
{ "slash_delimited_pattern" , test_slash_delimited_pattern},
{ "slash_delimited_literal_slashes" , test_slash_delimited_literal_slashes},
{ 0 }
};
Loading