Skip to content

Commit

Permalink
Fix SREMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerkaraszewski committed Jul 25, 2024
1 parent 2368ffa commit 9a52942
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
31 changes: 26 additions & 5 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2805,21 +2805,42 @@ bool SIsValidSQLiteDateModifier(const string& modifier) {
return true;
}

bool SREMatch(const string& regExp, const string& s) {
return pcrecpp::RE(regExp, pcrecpp::RE_Options().set_match_limit_recursion(1000)).FullMatch(s);
bool SREMatch(const string& regExp, const string& input, bool caseSensitive) {
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
uint32_t matchFlags = 0;

// These require full-string matches as that's the historical way this function works.
uint32_t compileFlags = PCRE2_ANCHORED | PCRE2_ENDANCHORED;
if (!caseSensitive) {
compileFlags |= PCRE2_CASELESS;
}
pcre2_match_context* matchContext = pcre2_match_context_create(0);
pcre2_set_depth_limit(matchContext, 1000);
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)regExp.c_str(), PCRE2_ZERO_TERMINATED, compileFlags, &errornumber, &erroroffset, 0);
pcre2_match_data* matchData = pcre2_match_data_create_from_pattern(re, 0);

int result = pcre2_match(re, (PCRE2_SPTR8)input.c_str(), input.size(), 0, matchFlags, matchData, matchContext);

pcre2_code_free(re);
pcre2_match_context_free(matchContext);
pcre2_match_data_free(matchData);

return result > 0;
}

string SREReplace(const string& regExp, const string& input, const string& replacement, bool caseSensitive) {
char* output = nullptr;
size_t outSize = 0;
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
int flags = PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
uint32_t compileFlags = caseSensitive ? 0 : PCRE2_CASELESS;
uint32_t substituteFlags = PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
pcre2_match_context* matchContext = pcre2_match_context_create(0);
pcre2_set_depth_limit(matchContext, 1000);
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)regExp.c_str(), PCRE2_ZERO_TERMINATED, caseSensitive ? 0 : PCRE2_CASELESS, &errornumber, &erroroffset, 0);
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)regExp.c_str(), PCRE2_ZERO_TERMINATED, compileFlags, &errornumber, &erroroffset, 0);
for (int i = 0; i < 2; i++) {
int result = pcre2_substitute(re, (PCRE2_SPTR8)input.c_str(), input.size(), 0, flags, 0, matchContext, (PCRE2_SPTR8)replacement.c_str(), replacement.size(), (PCRE2_UCHAR*)output, &outSize);
int result = pcre2_substitute(re, (PCRE2_SPTR8)input.c_str(), input.size(), 0, substituteFlags, 0, matchContext, (PCRE2_SPTR8)replacement.c_str(), replacement.size(), (PCRE2_UCHAR*)output, &outSize);
if (i == 0 && result == PCRE2_ERROR_NOMEMORY) {
// This is the expected case on the first run, there's not enough space to store the result, so we allocate the space and do it again.
output = (char*)malloc(outSize);
Expand Down
2 changes: 1 addition & 1 deletion libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ bool SConstantTimeEquals(const string& secret, const string& userInput);
bool SConstantTimeIEquals(const string& secret, const string& userInput);

// Perform a full regex match. The '^' and '$' symbols are implicit.
bool SREMatch(const string& regExp, const string& s);
bool SREMatch(const string& regExp, const string& input, bool caseSensitive = true);
string SREReplace(const string& regExp, const string& input, const string& replacement, bool caseSensitive = true);

// Redact values that should not be logged.
Expand Down
21 changes: 21 additions & 0 deletions test/tests/LibStuffTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct LibStuff : tpunit::TestFixture {
TEST(LibStuff::testBase32Conversion),
TEST(LibStuff::testContains),
TEST(LibStuff::testFirstOfMonth),
TEST(LibStuff::SREMatchTest),
TEST(LibStuff::SREReplaceTest),
TEST(LibStuff::SQResultTest)
)
Expand Down Expand Up @@ -639,6 +640,26 @@ struct LibStuff : tpunit::TestFixture {
ASSERT_EQUAL(SFirstOfMonth(timeStamp4, -25), "2018-06-01");
}

void SREMatchTest() {
// Basic case.
ASSERT_TRUE(SREMatch(".*cat.*", "this contains cat"));
ASSERT_FALSE(SREMatch(".*cat.*", "this does not"));

// Case sensitive but case doesn't match.
ASSERT_FALSE(SREMatch(".*CAT.*", "this contains cat"));

// Case-insensitive.
ASSERT_TRUE(SREMatch(".*CAT.*", "this contains cat", false));
ASSERT_FALSE(SREMatch(".*CAT.*", "this does not", false));

// Capture groups don't break internal code.
ASSERT_TRUE(SREMatch(".*cat.*", "(this) (contains) (cat)"));
ASSERT_FALSE(SREMatch(".*cat.*", "(this) (does) (not)"));

// Partial matches aren't counted.
ASSERT_FALSE(SREMatch("cat", "this contains cat"));
}

void SREReplaceTest() {
// This specifically tests multiple replacements and that the final string is longer than the starting string.
string from = "a cat is not a dog it is a cat";
Expand Down

0 comments on commit 9a52942

Please sign in to comment.