Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/phpunit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,25 @@ jobs:
- name: Run PHPUnit suite
run: php ./vendor/bin/phpunit -c ./phpunit.xml.dist
working-directory: packages/mysql-on-sqlite

- name: Check REGEXP matching with PCRE2 10.39 and JIT
if: matrix.php == '8.1'
run: |
docker run --rm -i --network none \
-v "$GITHUB_WORKSPACE:/project:ro" -w /project/packages/mysql-on-sqlite \
php:8.1.34-cli@sha256:76e563191d1ade120313a8736df24154d21da5155c0756f147c0b01bd19d9087 \
bash -eo pipefail <<'SH'
# This match succeeds only with JIT at the selected depth limit.
php -d pcre.jit=1 -d pcre.recursion_limit=100 -r '
exit(explode(" ", PCRE_VERSION)[0] === "10.39" &&
preg_match("/^(item,)+$/", str_repeat("item,", 100)) === 1 ? 0 : 1);
'
# Use ASCII depth tests to distinguish JIT from the interpreter on this old engine.
# The punctuation case checks a whole UTF-8 character on the JIT path.
php -d pcre.jit=1 vendor/bin/phpunit --do-not-cache-result \
--filter 'testRegexpNegated.*DepthLimit|testRegexpNegatedSetOfOneLetter.*punctuation' \
--log-junit /tmp/regexp.xml
for method in SetUsesNativeDepthLimit LetterUsesInterpreterDepthLimit SetOfOneLetter; do
grep -q "name=\"testRegexpNegated$method" /tmp/regexp.xml
done
SH
Original file line number Diff line number Diff line change
Expand Up @@ -1322,13 +1322,17 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
* characters like "ą" (0xC4 0x85). (*ANYCRLF) misses only the rare VT and FF.
*/
if ( $unix_lines ) {
$newline = '(*LF)';
$prefix = '(*LF)';
} elseif ( $binary_mode ) {
$newline = '(*ANYCRLF)';
$prefix = '(*ANYCRLF)';
} else {
$newline = '(*ANY)';
$prefix = '(*ANY)';
}
$compiled = '/' . $newline . $this->regexp_escape_pattern( $pattern, $without_g ) . '/' . $modifiers;

if ( $this->regexp_requires_no_jit( $pattern, $case_sensitive, $binary_mode ) ) {
$prefix = '(*NO_JIT)' . $prefix;
}
$compiled = '/' . $prefix . $this->regexp_escape_pattern( $pattern, $without_g ) . '/' . $modifiers;

/*
* Compile the pattern by matching it against an empty string. Only a compile
Expand All @@ -1353,8 +1357,44 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
}

/**
* Escape pattern delimiters and NUL bytes that are not already escaped, read
* "\K" as "K", and, if asked, make "\G" never match.
* Before PCRE2 10.40, JIT can split a UTF-8 character or crash PHP when
* a case-insensitive set excludes a single letter. Inline options can
* enable case folding or turn on UTF in a binary pattern.
*
* Before the caret, PCRE2 skips "\E" and empty "\Q\E" quotes. In "(?xx)"
* mode it also skips spaces and tabs. Keep JIT when the set clearly
* excludes a character with no case variants or multiple distinct characters.
*
* Quoted text can look like a set and select the interpreter too.
* Without JIT, long scans can slow down or hit a backtrack or depth
* limit. The version check also covers builds with a backported fix.
*
* @param string $pattern The MySQL pattern.
* @param bool $case_sensitive Whether matching starts in case-sensitive mode.
* @param bool $binary_mode Whether matching starts in byte mode.
* @return bool Whether to disable JIT for this pattern.
*/
private function regexp_requires_no_jit( $pattern, $case_sensitive, $binary_mode ) {
return PHP_VERSION_ID >= 70300 && false !== strpos( $pattern, '^' )
&& ( ! $binary_mode || false !== strpos( $pattern, '(*UTF' ) )
&& version_compare( explode( ' ', PCRE_VERSION )[0], '10.40', '<' )
&& ( ! $case_sensitive || 0 !== preg_match( '/\(\?\^?[a-zA-Z]*i/', $pattern ) )
&& 0 !== preg_match(
'~\[(?:[ \t]|\\\\E|\\\\Q\\\\E)*+\^ (?!
[ ]\] | (?![a-zA-Z\[\\\\])[\x21-\x7e] # Space alone, ASCII digit or punctuation.
| \\\\ (?:[dDhHsSvVwW]|(?=[\x21-\x7e])[[:punct:]]) # Shorthand class or escaped punctuation.
| \[:\^?[a-z]++:\] # POSIX class.
| ([a-zA-Z]) (?:
-(?!\1)[a-zA-Z0-9] # Distinct range endpoints.
| (?!(?i:\1)|[-\[\]\\\\])[\x21-\x7e] # A second literal distinct under /i.
)
)~x',
$pattern
);
}

/**
* Prepare a MySQL regex for use as a slash-delimited PCRE pattern.
*
* PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00".
*
Expand All @@ -1380,6 +1420,40 @@ private function regexp_escape_pattern( $pattern, $without_g = false ) {
$escapes['G'] = '\\b\\B';
}

/*
* ICU reads the hyphen in "[\w-.]" literally. PCRE2 rejects the range.
* Leave ICU set operations alone. Skip comments, quoted text and control
* escapes because they can hide set boundaries.
*
* Check the rest of the pattern too. Rewriting the hyphen could make
* PCRE2 accept syntax ICU rejects, such as a Python-style named group,
* a quantified lookahead, a bare "\N" or the property "\p{Xan}".
*/
$escape_hyphens = PHP_VERSION_ID >= 70300
&& false !== strpos( $pattern, '-' )
&& false !== strpos( $pattern, '\\' )
&& preg_match(
'~\A(?:
[^(\[\]{}\\\\#]++ | \((?:\?:|(?![?*])) |
\{[0-9]++(?:,[0-9]*+)?\} | \\\\[dDhHsSvVwWAbBGZzafnrtKRX[:punct:]] |
\[\^?(?:
[a-zA-Z0-9._](?:-[a-zA-Z0-9._])?+ |
\\\\[dDhHsSvVwW]-?+ | \\\\[.\]-] | (?<!-)-(?=\])
)++\]
)*+\z~x',
$pattern
);

/*
* PCRE 8 has no "(*NO_JIT)". Its JIT can split a UTF-8 character when
* matching "[^s]" case-insensitively. Writing "[^ss]" avoids the bug.
* Equivalent forms such as "[^\x73]" and "[^s-s]" can still split a character.
*
* Skip patterns with "\c". In "\c\\Q", the control escape consumes
* the first backslash, so the second backslash starts a "\Q" quote.
*/
$splits_characters = PHP_VERSION_ID < 70300 && false === strpos( $pattern, '\c' );
Comment thread
JanJakes marked this conversation as resolved.
Resolved

$escaped = '';
$quoted = false;
$length = strlen( $pattern );
Expand All @@ -1402,6 +1476,18 @@ private function regexp_escape_pattern( $pattern, $without_g = false ) {
$escaped .= isset( $escapes[ $next ] ) ? $escapes[ $next ] : $character . $next;
$quoted = 'Q' === $next;
++$i;

if ( $escape_hyphens && false !== strpos( 'dDhHsSvVwW', $next ) && '-' === substr( $pattern, $i + 1, 1 ) ) {
$escaped .= '\\-';
++$i;
}
} elseif (
$splits_characters && '[' === $character
// (*NO_START_OPT) avoids searching for "]" at every "[".
&& preg_match( '/(*NO_START_OPT)\[\^([a-zA-Z]|[\xC2-\xF4][\x80-\xBF]{1,3})\]/A', $pattern, $match, 0, $i )
) {
$escaped .= '[^' . $match[1] . $match[1] . ']';
$i += strlen( $match[0] ) - 1;
} elseif ( isset( $special[ $character ] ) ) {
$escaped .= $special[ $character ];
} else {
Expand Down
Loading
Loading