Skip to content

Reject a regular expression that ends in a lone backslash - #118634

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
regexp-trailing-backslash-117853
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
regexp-trailing-backslash-117853

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Closes: #117853

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A regular expression that ends in a lone backslash is now rejected as invalid by match, extractAll, countMatches and splitByRegexp, instead of being answered as a search for the pattern without the backslash.

Description

match('abcd', 'abc\\') returned 1 and match(s, '\\') returned 1 for every row. A trailing backslash is invalid in re2 (trailing \), but the pattern analyzer consumed it at the end of the pattern and stopped, leaving the pattern classified as a trivial literal - and everything trivial is answered by a substring search, so re2, which would have rejected the pattern, never saw it. The same pattern is rejected by replaceRegexpAll, by multiMatchAny, and by match itself in any non-trivial spelling (^abc\, ab(c)\).

OptimizedRegularExpression::analyzeImpl now marks such a pattern non-trivial, so it is compiled and reported as CANNOT_COMPILE_REGEXP, like every other invalid regular expression. Escapes with a character after the backslash are unaffected.

01866_split_by_regexp asserted the old answer for splitByRegexp('\\', 'a\\b\\c') - the whole string, unsplit - next to the same assertion for the other invalid patterns (+, ?, (, [), which already expect CANNOT_COMPILE_REGEXP; it now expects that too.


Workflow [PR]
Sync PR [sync-upstream/pr/118634]

`match('abcd', 'abc\\')` returned `1`, and `match(s, '\\')` returned `1` for
every row. A trailing backslash is invalid in re2 (`trailing \`), but the
pattern analyzer consumed it at the end of the pattern and stopped, leaving the
pattern classified as a trivial literal - and everything trivial is answered by
a substring search, so re2, which would have rejected the pattern, never saw it.
The same pattern is rejected by `replaceRegexpAll`, by `multiMatchAny`, and by
`match` itself in any non-trivial spelling (`^abc\`, `ab(c)\`).

`analyzeImpl` now marks such a pattern non-trivial, so it is compiled and
reported as `CANNOT_COMPILE_REGEXP`, like every other invalid regular
expression. Escapes with a character after the backslash are unaffected.

`01866_split_by_regexp` asserted the old answer for
`splitByRegexp('\\', 'a\\b\\c')` - the whole string, unsplit - next to the same
assertion for the other invalid patterns (`+`, `?`, `(`, `[`), which already
expect `CANNOT_COMPILE_REGEXP`; it now expects that too.

Closes: #117853

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [65b4724]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR fixes the OptimizedRegularExpression trivial-pattern fast path so a regexp that ends with a lone backslash is no longer silently downgraded to a literal substring search and instead reaches re2, which rejects it with CANNOT_COMPILE_REGEXP. The code change is surgical, matches the bug report, and the added stateless coverage exercises the previously broken user-visible cases along with valid escaped-backslash patterns.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 65b4724.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.10% 81.20% +0.10%

Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Sep 7, 2026
@rschu1ze

rschu1ze commented Sep 8, 2026

Copy link
Copy Markdown
Member

Duplicate of #118229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

2 participants