Skip to content

Commit 891d348

Browse files
authored
regex_remap: convert from pcre to Regex (#12575)
* regex_remap: convert from pcre to Regex * get RegexMatchContext helper class working * cleanup * remove pcre2 match context features not in centos * add back in comments from the pcre plugin * refactor Regex::captureCount(), add unit tests * change Regex api function convention, add to unit test * better handling of regex exec call failure * use errmsg in diags.log, restore older match limit test
1 parent a2326be commit 891d348

9 files changed

Lines changed: 380 additions & 102 deletions

File tree

include/tsutil/Regex.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,41 @@ class RegexMatches
9494
_MatchDataPtr _match_data;
9595
};
9696

97+
/// @brief Wrapper for PCRE2 match context
98+
///
99+
/// @internal This instance is not tied to any Regex and can be used with one of the Regex::exec overloads.
100+
class RegexMatchContext
101+
{
102+
friend class Regex;
103+
104+
public:
105+
/** Construct a new RegexMatchContext object.
106+
*/
107+
RegexMatchContext();
108+
~RegexMatchContext();
109+
110+
/// uses pcre2_match_context_copy for a deep copy.
111+
RegexMatchContext(RegexMatchContext const &orig);
112+
RegexMatchContext &operator=(RegexMatchContext const &orig);
113+
114+
RegexMatchContext(RegexMatchContext &&) = default;
115+
RegexMatchContext &operator=(RegexMatchContext &&) = default;
116+
117+
/** Limits the amount of backtracking that can take place.
118+
* Any regex exec call that fails will return PCRE2_ERROR_MATCHLIMIT(-47)
119+
*/
120+
void set_match_limit(uint32_t limit);
121+
122+
private:
123+
/// @internal This wraps a void* so to avoid requiring a pcre2 include.
124+
struct _MatchContext;
125+
struct _MatchContextPtr {
126+
void *_ptr = nullptr;
127+
};
128+
129+
_MatchContextPtr _match_context;
130+
};
131+
97132
/// @brief Wrapper for PCRE2 regular expression.
98133
class Regex
99134
{
@@ -179,17 +214,28 @@ class Regex
179214
* @param subject String to match against.
180215
* @param matches Place to store the capture groups.
181216
* @param flags Match flags (e.g., RE_NOTEMPTY).
217+
* @param optional context Match context (set matching limits).
182218
* @return @c The number of capture groups. < 0 if an error occurred. 0 if the number of Matches is too small.
183219
*
184220
* It is safe to call this method concurrently on the same instance of @a this.
185221
*
186222
* Each capture group takes 3 elements of @a ovector, therefore @a ovecsize must
187223
* be a multiple of 3 and at least three times the number of desired capture groups.
188224
*/
189-
int exec(std::string_view subject, RegexMatches &matches, uint32_t flags) const;
225+
int exec(std::string_view subject, RegexMatches &matches, uint32_t flags,
226+
RegexMatchContext const *const matchContext = nullptr) const;
227+
228+
/** Error string for exec failure.
229+
*
230+
* @param int return code from exec call.
231+
*/
232+
static std::string get_error_string(int rc);
233+
234+
/// @return The number of capture groups in the compiled pattern, -1 for fail.
235+
int32_t get_capture_count() const;
190236

191-
/// @return The number of capture groups in the compiled pattern.
192-
int get_capture_count();
237+
/// @return number of highest back references, -1 for fail.
238+
int32_t get_backref_max() const;
193239

194240
/// @return Is the compiled pattern empty?
195241
bool empty() const;

plugins/experimental/cookie_remap/cookie_remap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class subop
445445

446446
Regex *regex = nullptr;
447447
std::string regex_string;
448-
int regex_ccount = 0;
448+
int32_t regex_ccount = 0;
449449

450450
std::string bucket;
451451
unsigned int how_many = 0;

plugins/regex_remap/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
add_atsplugin(regex_remap regex_remap.cc)
1919

20-
target_link_libraries(regex_remap PRIVATE PCRE::PCRE libswoc::libswoc)
20+
target_link_libraries(regex_remap PRIVATE libswoc::libswoc)
2121

2222
verify_remap_plugin(regex_remap)

0 commit comments

Comments
 (0)