Repository navigation
fix(parse): prevent unintended Unicode case folding for character classes in alternations - #681
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
When character classes like `[sS]` and `[kK]` are parsed in UTF-8 mode without the case-insensitive flag (`FoldCase`), `PushRegexp` rewrote them as literals with `flags_ | FoldCase` if their size was 2 and contained matching uppercase and lowercase ASCII characters. However, in UTF-8 mode, Unicode case folding on `s` and `k` includes non-ASCII runes (U+017F / Latin small letter long s, and U+212A / Kelvin sign). When an alternation like `a|[sS]` or `a|[kK]` was subsequently factored in `FactorAlternationImpl::Round3`, the literal's `FoldCase` flag caused `AddRangeFlags` to pull in those non-ASCII fold variants into the combined character class, causing `a|[sS]` to match `\u017F` and `a|[kK]` to match `\u212A`, while `[sS]` alone, `[kK]` alone, or `a|s|S` did not. Only rewrite two-character ASCII classes to literals with `FoldCase` if `Latin1` mode is enabled or if the lower/upper runes form an exact 2-cycle in case folding (`CycleFoldRune(lower) == upper`), preventing `k` and `s` from erroneously acquiring Unicode case-fold expansion when no fold flag was requested. Fixes google#676. Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In UTF-8 mode without the case-insensitive flag (
FoldCase),a|[sS]matches\u017F(Latin small letter long s) anda|[kK]matches\u212A(Kelvin sign), while[sS]alone,[kK]alone, anda|s|Sdo not match.Cause
In
Regexp::ParseState::PushRegexp(re2/parse.cc), character classes of size 2 containing an uppercase/lowercase ASCII letter pair are rewritten as literals withflags_ | FoldCase.However, in UTF-8 mode, Unicode case folding on
sandkincludes non-ASCII runes (\u017Fand\u212A). When alternations likea|[sS]ora|[kK]are subsequently factored inFactorAlternationImpl::Round3, the literal'sFoldCaseflag causesAddRangeFlagsto expandsandkinto their full Unicode folding cycles, injecting the non-ASCII runes into the merged character class.Solution
In
PushRegexp, only rewrite two-character ASCII classes to literals withFoldCaseifLatin1mode is enabled or if the lower/upper runes form an exact 2-cycle in case folding (CycleFoldRune(lower) == upper). This preserves the optimization for ASCII letters that only fold between uppercase and lowercase ASCII, while preventingkandsfrom erroneously acquiring Unicode case-folding expansion when case-insensitivity was not enabled.Added regression tests verifying that
[sS],a|[sS],[kK], anda|[kK]do not match\u017For\u212A.Fixes #676.