I have found a case where e.g. 500 letters/digits with whitespace makes PCRE blow up when using \w in the regex. I have tried boh TPerlRegEx and Delphi XE2 pcre/obj implementations. Same for both.
I then tried calling
pcre_config(PCRE_CONFIG_MATCH_LIMIT, @vSysStrRegex_MatchLimit_Value);
But setting match limits does not seem to have any effect. The way I am using it is that I call it once for each thread. (Note: Others have set this to get not set such settings)
I really need the regex library to drop out of parsing instead of continuing until it overflows the stack. (It seems next to impossible to recover the thread/program from that.)
How do I prevent stack overflows in this situation? I can’t control the content parsed or the regular expressions. Thus I am specificly looking for ways to avoid PCRE running into stack overflows through a setting or similar.
Solution by editing TPerlRegEx code:
function TPerlRegEx.Match(AStartOffset: Integer = 0): Boolean;
...
if FHints <> nil then // set by "study" call
begin
PPCREExtra(FHints)^.flags := PPCREExtra(FHints)^.flags or PCRE_EXTRA_MATCH_LIMIT_RECURSION;
PPCREExtra(FHints)^.match_limit_recursion := 750 // 1000 too much in tests
end
;
OffsetCount := pcre_exec(FPattern, FHints, ...)
You’ve cited some PCRE documentation that describes setting the recursion limit at compile time with the
--with-match-limit-recursionconfiguration option. You can exercise that option if you build the PCRE library yourself. If you read elsewhere in that same document, you’ll find a description of thematch_limit_recursionfield of thepcre_extrablock passed topcre_exec:So, set the recursion limit to something lower than the default. The default is evidently even higher than your actual available stack space; if it were lower, then you’d already be getting the
PCRE_ERROR_RECURSIONLIMITerror instead of an OS-raised stack-overflow exception.How the Delphi-specific wrappers represent this setting, I have no idea.