From 8fcf670102d4046dd026f9c0cd183ff83d0b3b5d Mon Sep 17 00:00:00 2001 From: Michael Duda Date: Wed, 20 May 2026 12:00:15 -0600 Subject: [PATCH 1/2] Provide optimized code path in check_regex_match function for non-regex strings When the 'pattern' argument to the check_regex_match function in regex_matching.c is not a regular expression, a simple string comparison is sufficient and the cost of compiling a regular expression can be avoided. This commit adds an optimized code path in the check_regex_match function for this case. --- src/framework/regex_matching.c | 43 ++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/framework/regex_matching.c b/src/framework/regex_matching.c index d37a23436d..ae62d09a5d 100644 --- a/src/framework/regex_matching.c +++ b/src/framework/regex_matching.c @@ -1,14 +1,54 @@ #include #include #include +#include #define MAX_LEN 1024 -void check_regex_match(const char * pattern, const char * str, int *imatch){ +static const char *BRE_SPECIAL_CHARS = ".[\\*^$"; + +/****************************************************************************** + * + * possible_bre + * + * Determine whether an input string may be a Basic Regular Expression (BRE). + * This function may return false positives (i.e., a return value indicating + * that a string is a BRE, when in fact the string is not a BRE), but it will + * never return a false negative (i.e., a return value indicating that a string + * is not a BRE, when in fact the string is a BRE). + * + * Inputs: + * s - a null-terminated string + * + * Return value: 1 if the input string 's' is possibly a BRE and 0 otherwise. + * + ******************************************************************************/ +static int possible_bre(const char *s) +{ + for (; *s != '\0'; s++) { + if (strchr(BRE_SPECIAL_CHARS, *s) != NULL) { + return 1; + } + } + return 0; +} + + +void check_regex_match(const char * pattern, const char * str, int *imatch) +{ regex_t regex; char bracketed_pattern[MAX_LEN]; int ierr, len; + /* + * If pattern is a simple string and not a basic regular expression, + * a string comparison will suffice + */ + if (!possible_bre(pattern)) { + *imatch = (strcmp(pattern, str) == 0) ? 1 : 0; + return; + } + *imatch = 0; len = snprintf(bracketed_pattern, 1024, "^%s$", pattern); if ( len >= MAX_LEN ) { @@ -34,4 +74,3 @@ void check_regex_match(const char * pattern, const char * str, int *imatch){ *imatch = -1; } } - From 384dff94b833e5693f1bcc7ea487042d701ff635 Mon Sep 17 00:00:00 2001 From: Michael Duda Date: Mon, 8 Jun 2026 16:58:41 -0600 Subject: [PATCH 2/2] Add documentation for the check_regex_match function in regex_matching.c --- src/framework/regex_matching.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/framework/regex_matching.c b/src/framework/regex_matching.c index ae62d09a5d..17136507a7 100644 --- a/src/framework/regex_matching.c +++ b/src/framework/regex_matching.c @@ -34,6 +34,23 @@ static int possible_bre(const char *s) } +/****************************************************************************** + * + * check_regex_match + * + * Determine whether an input string 'str' matches the Basic Regular Expression + * (BRE) 'pattern'. + * + * Inputs: + * pattern - a null-terminated string that may contain any valid BRE, + * including a simple string + * str - a null-terminated string to be checked against pattern + * + * Return value: 1 if the input string str matches the BRE pattern, + * 0 if the input string does not match the BRE pattern, and + * -1 if an error occurred. + * + ******************************************************************************/ void check_regex_match(const char * pattern, const char * str, int *imatch) { regex_t regex;