Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regexp: wrong match with 'ic' and comparing multi-byte with single byte char #14756

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chrisbra
Copy link
Member

This is v2 of v9.1.296, but still draft to see if this still breaks.

When the regexp engine compares two utf-8 codepoints case insensitively it may match an adjacent character, because it assumes it can step over as many bytes as the pattern contains.

This however is not necessarily true because of case-folding, a multi-byte UTF-8 character can be considered equal to some single-byte value.

Let's consider the pattern 'ſ' and the string 's'. When comparing and ignoring case, the single character 's' matches, and since it matches Vim will try to step over the match (by the amount of bytes of the pattern), assuming that since it matches, the length of both strings is the same.

However in that case, it should only step over the single byte value 's' by 1 byte and try to start matching after it again. So for the backtracking engine we need to ensure:

  • we try to match the correct length for the pattern and the text
  • in case of a match, we step over it correctly

There is one tricky thing for the backtracing engine.
We also need to calculate correctly the number of bytes to compare the 2 different utf-8 strings s1 and s2. So we will count the number of characters in s1 that the byte len specified. Then we count the number of bytes to step over the same number of characters in string s2 and then we can correctly compare the 2 utf-8 strings.

A similar thing can happen for the NFA engine, when skipping to the next character to test for a match. We are skipping over the regstart pointer, however we do not consider the case that because of case-folding we may need to adjust the number of bytes to skip over. So this needs to be adjusted in find_match_text() as well.

A related issue turned out, when prog->match_text is actually empty. In that case we should try to find the next match and skip this condition.

Note: this breaks Mail and CSS Syntax highlighting and CI on FreeBSD/MacOS #14487 and
https://groups.google.com/d/msgid/vim_dev/CAJkCKXtui%3DDTWx9eV8Dbs19XoFL9b63ObSNXWCRvLsEZCB6yfw%40mail.gmail.com.

fixes: #14294

…te char

This is v2 of v9.1.296, but still draft to see if this still breaks.

When the regexp engine compares two utf-8 codepoints case insensitively it may
match an adjacent character, because it assumes it can step over as many bytes
as the pattern contains.

This however is not necessarily true because of case-folding, a multi-byte
UTF-8 character can be considered equal to some single-byte value.

Let's consider the pattern 'ſ' and the string 's'. When comparing and ignoring
case, the single character 's' matches, and since it matches Vim will try to
step over the match (by the amount of bytes of the pattern), assuming that
since it matches, the length of both strings is the same.

However in that case, it should only step over the single byte value 's' by 1
byte and try to start matching after it again. So for the backtracking engine
we need to ensure:
- we try to match the correct length for the pattern and the text
- in case of a match, we step over it correctly

There is one tricky thing for the backtracing engine. We also need to calculate
correctly the number of bytes to compare the 2 different utf-8 strings s1 and
s2. So we will count the number of characters in s1 that the byte len
specified. Then we count the number of bytes to step over the same number of
characters in string s2 and then we can correctly compare the 2 utf-8 strings.

A similar thing can happen for the NFA engine, when skipping to the next
character to test for a match. We are skipping over the regstart pointer,
however we do not consider the case that because of case-folding we may need to
adjust the number of bytes to skip over. So this needs to be adjusted in
find_match_text() as well.

A related issue turned out, when prog->match_text is actually empty. In that
case we should try to find the next match and skip this condition.

Note: this breaks Mail and CSS Syntax highlighting and CI on FreeBSD/MacOS
vim#14487 and
https://groups.google.com/d/msgid/vim_dev/CAJkCKXtui%3DDTWx9eV8Dbs19XoFL9b63ObSNXWCRvLsEZCB6yfw%40mail.gmail.com.

fixes: vim#14294

Signed-off-by: Christian Brabandt <cb@256bit.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unicode character ſ is matched as itself and as 's.'
1 participant