Boost.Regex
Describes how recheck checks a regular expression of the boost-regex dialect.
Engine
recheck follows the syntax and the semantics of the perl syntax of Boost.Regex 1.92.0 (boost::regex_constants::perl, which is the default and the same as ECMAScript), searched by regex_search with match_default and the given match flags (see Flags and Units).
The POSIX syntaxes (e.g. extended and basic) are not supported.
Settings
typeis the character type of the pattern and its input:char(default) forboost::regex, oru32regexforboost::u32regex, which uses ICU.wchar_t(boost::wregex) is not supported.
- CLI
- JavaScript
- Scala
$ recheck --dialect=boost-regex '/^(a|a)*$/'
$ recheck --dialect=boost-regex --boost-regex-type=u32regex '/^(a|a)*$/'
import { check } from "recheck";
await check("^(a|a)*$", "", { dialect: "boost-regex" });
await check("^(a|a)*$", "", {
dialect: { name: "boost-regex", type: "u32regex" },
});
import codes.quine.labs.recheck.ReDoS
import codes.quine.labs.recheck.common.Parameters
import codes.quine.labs.resyntax.dialect.Dialect
ReDoS.check("^(a|a)*$", "", Parameters(dialect = Dialect.BoostRegex()))
ReDoS.check("^(a|a)*$", "", Parameters(dialect = Dialect.BoostRegex(Dialect.BoostRegex.Type.U32Regex)))
Flags and Units
The matching unit is byte for char (the default type), and scalar-value for u32regex (see Matching Units).
recheck supports the following syntax options and match flags of Boost.Regex, written as these flag letters.
| Flag | Option | Inline | Notes |
|---|---|---|---|
i | icase | (?i) | |
x | mod_x | (?x) | |
M | no_mod_m | (?-m) | ^ and $ match at the line terminators without it. |
S | no_mod_s | (?-s) | . matches the line terminators without it. |
N | newline_alt | A line feed in the pattern is |. | |
c | collate | Not supported with a range of a class. | |
p | match_posix | The longest match at a start position. An error with (*COMMIT), (*PRUNE), (*SKIP) or (*THEN). | |
E | match_not_null | Rejects an empty match. | |
I | match_not_initial_null | Rejects an empty match at the start of the input. | |
Z | match_not_dot_null | . does not match NUL, even with (?s). | |
L | match_single_line | ^ and $ match only at the start and the end of the input, even with (?m). |
As in Boost.Regex, ^ and $ are multi-line and . matches the line terminators by default, and $ without multi-line matches only at the end, as \z does.
The line terminators are \n, \f and \r, and also U+0085, U+2028 and U+2029 in u32regex, and neither ^ nor $ matches between \r and \n.
\Z matches before any line terminators at the end (e.g. a\Z matches "a\n\r").
The other syntax options (e.g. nosubs and no_empty_expressions) do not change how a valid pattern matches, so they have no flag letter.
mod_s has no flag letter either, since . matches the line terminators by default.
match_not_dot_newline is the same as S, and the other match flags (e.g. match_not_bol) cannot be given.
Over char, the classes are of the C locale (e.g. \w is [0-9A-Za-z_]), and i matches the ASCII letters case-insensitively.
In u32regex, the classes are of ICU's general categories as Boost.Regex defines them (e.g. \w is \p{L}, \p{Nd}, \p{Mn} and _), and i is the simple case folding.
\p{...} takes the class names of Boost.Regex (e.g. \p{alpha}), and in u32regex the names of the general categories too, where a class name comes first (e.g. \pL is \p{lower}).
Under i, a class matches a character whose case folding it has, and a class of the lower or the upper case letters has all the letters.
For example, (?i)[@-Z] matches "[", and (?i)\p{Lt} matches no character, since an Lt character folds to an Ll one.
Syntax Limitations
The following constructs are not supported, and they are reported as unsupported.
- Recursion and calls of groups (e.g.
(?R),(?1),(?&name)and(?P>name)). - Conditionals (e.g.
(?(1)a|b),(?(?=a)a|b)and(?(DEFINE)...)). - The backtracking control verbs other than
(*FAIL)and(*F)(e.g.(*PRUNE)and(*ACCEPT)). - The constructs depending on the collation of the locale: a range of a class with
c, the collating elements (e.g.[[.space.]]) and the equivalence classes (e.g.[[=a=]]). \N{...}(e.g.\N{newline}), which is a collating element.
Approximations
- Boost.Regex stops a matching with
error_complexitywhen it takes too many steps, and witherror_stackwhen its backtracking stack overflows, whichrecheckdoes not model. The limit of the steps grows with the lengths of the input and of the pattern, so most attacks of a short pattern end with the limit in less than a second, and the runtime-attack verification reports them as an engine limit. The stack overflows even on a linear matching (e.g.^(a)*$on"a"repeated 100,000 times). recheckdoes not reproduce the following behaviors of Boost.Regex.- A back-reference inside its own group matches the empty string, since Boost.Regex sets only the start of the capture on entering the group (e.g.
^(a\1?){4}$does not match"aaaaaaaaaa");recheckmatches the capture of the previous iteration. - The captures in an atomic group or a look-ahead are not restored on backtracking (e.g.
(?>(a))b|\1matches"ac");recheckrestores them. - In
u32regex, the line terminators and\vare compared by the low 16 bits of a code point (e.g. U+12028 is a line terminator);recheckcompares the code points. - In
u32regex,[[:print:]]matches\t,\nand the other spaces of ICU'su_isspacetoo;recheckmatches the characters out of the general categoryC. - The negated escape classes in a class are joined before the negation (e.g.
[\D\W]does not match"a");recheckjoins them as the other items. - Under
i,\Xtakes U+0345 as not a combining character, since its case folding U+03B9 is not one;rechecktakes it as one. - A quantifier after
(?i)or(?-i), which repeats it as an empty group, undoes the change ofifor the characters after it, but not for the classes (e.g.a(?i)+bdoes not match"aB", buta(?i)+[b]does);recheckchangesifor both.
- A back-reference inside its own group matches the empty string, since Boost.Regex sets only the start of the capture on entering the group (e.g.
Runtime-Attack Verification
- Command:
c++ <file>.cpp -o <file>once, with-licui18n -licuuc -licudataafter it foru32regex, and then the compiled program. - Matching:
regex_searchwithmatch_results(u32regex_searchforu32regex),match_defaultand the given match flags, with Boost.Regex's default limits. - Engine limits:
error_complexity. - Engine errors:
error_stack,std::bad_alloc. - Environment variable:
RECHECK_BOOST_REGEX.
RECHECK_BOOST_REGEX replaces c++ with a command line of a C++ compiler, which finds the headers of Boost, and ICU's headers and libraries for u32regex.
recheck adds the program, -o, the compiled program and ICU's libraries after it.
Boost.Regex needs no library of its own.
The headers of Boost.Regex alone (e.g. of the boostorg/regex repository) are enough with boost/version.hpp of Boost.Config and -DBOOST_REGEX_STANDALONE, which keeps Boost.Regex from using the other libraries of Boost.
For example, the following uses Boost and ICU of Homebrew.
$ export RECHECK_BOOST_REGEX="c++ -std=c++17 -O2 -I$(brew --prefix)/include -I$(brew --prefix icu4c@79)/include -L$(brew --prefix icu4c@79)/lib"
$ recheck --dialect=boost-regex --runtime-attack-timeout=1s '/^(a|a)*$/'
recheck compiles the program once for each command line and version of the compiler, and the next runs reuse the compiled program.
-O2 matters, since the matching without optimizations takes about ten times as long.
A macro of Boost.Regex on the command line (e.g. -DBOOST_REGEX_MAX_STATE_COUNT=1000000000) changes the limits as in your program.
The runtime is boost-regex with the version of Boost (e.g. boost-regex 1.92.0).
A failed compilation makes the runtime-attack verification fail with an unexpected error telling the output of the compiler.