|PCRESTACK(3)||Library Functions Manual||PCRESTACK(3)|
Not all calls of match() increase the recursion depth; for an item such as a* it may be called several times at the same level, after matching different numbers of a's. Furthermore, in a number of cases where the result of the recursive call would immediately be passed back as the result of the current call (a "tail recursion"), the function is just restarted instead.
The above comments apply when pcre[16|32]_exec() is run in its normal interpretive manner. If the pattern was studied with the PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was successful, and the options passed to pcre[16|32]_exec() were not incompatible, the matching process uses the JIT-compiled code instead of the match() function. In this case, the memory requirements are handled entirely differently. See the pcrejit documentation for details.
The pcre[16|32]_dfa_exec() function operates in an entirely different way, and uses recursion only when there is a regular expression recursion or subroutine call in the pattern. This includes the processing of assertion and "once-only" subpatterns, which are handled like subroutine calls. Normally, these are never very deep, and the limit on the complexity of pcre[16|32]_dfa_exec() is controlled by the amount of workspace it is given. However, it is possible to write patterns with runaway infinite recursions; such patterns will cause pcre[16|32]_dfa_exec() to run out of stack. At present, there is no protection against this.
The comments that follow do NOT apply to pcre[16|32]_dfa_exec(); they are relevant only for pcre[16|32]_exec() without the JIT optimization.
It matches from wherever it starts until it encounters "<inet" or the end of the data, and is the kind of pattern that might be used when processing an XML file. Each iteration of the outer parentheses matches either one character that is not "<" or a "<" that is not followed by "inet". However, each time a parenthesis is processed, a recursion occurs, so this formulation uses a stack frame for each matched character. For a long string, a lot of stack is required. Consider now this rewritten pattern, which matches exactly the same strings:
This uses very much less stack, because runs of characters that do not contain "<" are "swallowed" in one item inside the parentheses. Recursion happens only when a "<" character that is not followed by "inet" is encountered (and we assume this is relatively rare). A possessive quantifier is used to stop any backtracking into the runs of non-"<" characters, but that is not related to stack usage.
This example shows that one way of avoiding stack problems when matching long subject strings is to write repeated parenthesized subpatterns to match more than one character whenever possible.
As a very rough rule of thumb, you should reckon on about 500 bytes per recursion. Thus, if you want to limit your stack usage to 8Mb, you should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can support around 128000 recursions.
In Unix-like environments, the pcretest test program has a command line option (-S) that can be used to increase the size of its stack. As long as the stack is large enough, another option (-M) can be used to find the smallest limits that allow a particular pattern to match a given subject string. This is done by calling pcre[16|32]_exec() repeatedly with different limits.
pcretest -m -C
The -C option causes pcretest to output information about the options with which PCRE was compiled. When -m is also given (before -C), information about stack use is given in a line like this:
Match recursion uses stack: approximate frame size = 640 bytes
The value is approximate because some recursions need a bit more (up to perhaps 16 more bytes).
If the above command is given when PCRE is compiled to use the heap instead of the stack for recursion, the value that is output is the size of each block that is obtained from the heap.
Unfortunately, the effect of running out of stack is often SIGSEGV, though sometimes a more explicit error message is given. You can normally increase the limit on stack size by code such as this:
struct rlimit rlim; getrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = 100*1024*1024; setrlimit(RLIMIT_STACK, &rlim);
This reads the current limits (soft and hard) using getrlimit(), then attempts to increase the soft limit to 100Mb using setrlimit(). You must do this before calling pcre[16|32]_exec().
Philip Hazel University Computing Service Cambridge CB2 3QH, England.
Last updated: 24 June 2012 Copyright (c) 1997-2012 University of Cambridge.
|24 June 2012||PCRE 8.30|