aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/regexp.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-04-10 07:08:49 +0800
committerGitHub <noreply@github.com>2024-04-10 07:08:49 +0800
commitf49408454ddb48016d51b48bcd9d5dab538f5cc7 (patch)
tree815676873f69c142fd4492ff4482bf1ab80c13b9 /src/nvim/regexp.c
parent7142c5dde939f6675cba4f02f616d94c1174cde2 (diff)
downloadrneovim-f49408454ddb48016d51b48bcd9d5dab538f5cc7.tar.gz
rneovim-f49408454ddb48016d51b48bcd9d5dab538f5cc7.tar.bz2
rneovim-f49408454ddb48016d51b48bcd9d5dab538f5cc7.zip
vim-patch:9.1.0296: regexp: engines do not handle case-folding well (#28259)
Problem: Regex engines do not handle case-folding well Solution: Correctly calculate byte length of characters to skip 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' so 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 The same 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. fixes: vim/vim#14294 closes: vim/vim#14433 https://github.com/vim/vim/commit/7a27c108e0509f3255ebdcb6558e896c223e4d23 Co-authored-by: Christian Brabandt <cb@256bit.org>
Diffstat (limited to 'src/nvim/regexp.c')
-rw-r--r--src/nvim/regexp.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c
index 08c804bca5..32fb086ca6 100644
--- a/src/nvim/regexp.c
+++ b/src/nvim/regexp.c
@@ -1627,7 +1627,9 @@ static void mb_decompose(int c, int *c1, int *c2, int *c3)
/// Compare two strings, ignore case if rex.reg_ic set.
/// Return 0 if strings match, non-zero otherwise.
-/// Correct the length "*n" when composing characters are ignored.
+/// Correct the length "*n" when composing characters are ignored
+/// or for utf8 when both utf codepoints are considered equal because of
+/// case-folding but have different length (e.g. 's' and 'ſ')
static int cstrncmp(char *s1, char *s2, int *n)
{
int result;
@@ -1635,8 +1637,11 @@ static int cstrncmp(char *s1, char *s2, int *n)
if (!rex.reg_ic) {
result = strncmp(s1, s2, (size_t)(*n));
} else {
- assert(*n >= 0);
- result = mb_strnicmp(s1, s2, (size_t)(*n));
+ int l2 = utfc_ptr2len(s2);
+ result = utf_strnicmp(s1, s2, (size_t)(*n), (size_t)l2);
+ if (result == 0 && l2 < *n) {
+ *n = l2;
+ }
}
// if it failed and it's utf8 and we want to combineignore:
@@ -6490,11 +6495,9 @@ static bool regmatch(uint8_t *scan, const proftime_T *tm, int *timed_out)
}
}
} else {
- for (i = 0; i < len; i++) {
- if (opnd[i] != rex.input[i]) {
- status = RA_NOMATCH;
- break;
- }
+ if (cstrncmp((char *)opnd, (char *)rex.input, &len) != 0) {
+ status = RA_NOMATCH;
+ break;
}
}
rex.input += len;
@@ -13845,23 +13848,26 @@ static int skip_to_start(int c, colnr_T *colp)
// Returns zero for no match, 1 for a match.
static int find_match_text(colnr_T *startcol, int regstart, uint8_t *match_text)
{
-#define PTR2LEN(x) utf_ptr2len(x)
-
colnr_T col = *startcol;
- int regstart_len = PTR2LEN((char *)rex.line + col);
+ const int regstart_len = utf_char2len(regstart);
while (true) {
bool match = true;
uint8_t *s1 = match_text;
- uint8_t *s2 = rex.line + col + regstart_len; // skip regstart
+ // skip regstart
+ uint8_t *s2 = rex.line + col + regstart_len;
+ if (regstart_len > 1
+ && utf_char2len(utf_ptr2char((char *)rex.line + col)) != regstart_len) {
+ // because of case-folding of the previously matched text, we may need
+ // to skip fewer bytes than utf_char2len(regstart)
+ s2 = rex.line + col + utf_char2len(utf_fold(regstart));
+ }
while (*s1) {
- int c1_len = PTR2LEN((char *)s1);
+ int c1_len = utf_ptr2len((char *)s1);
int c1 = utf_ptr2char((char *)s1);
- int c2_len = PTR2LEN((char *)s2);
+ int c2_len = utf_ptr2len((char *)s2);
int c2 = utf_ptr2char((char *)s2);
-
- if ((c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2)))
- || c1_len != c2_len) {
+ if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2))) {
match = false;
break;
}
@@ -13894,8 +13900,6 @@ static int find_match_text(colnr_T *startcol, int regstart, uint8_t *match_text)
*startcol = col;
return 0L;
-
-#undef PTR2LEN
}
static int nfa_did_time_out(void)
@@ -15527,7 +15531,7 @@ static int nfa_regexec_both(uint8_t *line, colnr_T startcol, proftime_T *tm, int
// If match_text is set it contains the full text that must match.
// Nothing else to try. Doesn't handle combining chars well.
- if (prog->match_text != NULL && !rex.reg_icombine) {
+ if (prog->match_text != NULL && *prog->match_text != NUL && !rex.reg_icombine) {
retval = find_match_text(&col, prog->regstart, prog->match_text);
if (REG_MULTI) {
rex.reg_mmatch->rmm_matchcol = col;