Commit 9b245eb2

9b245eb2db6eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

regexp: import mruby bug fixes + pick best match per Ruby semantics

Three regex engine fixes:

  1. re_compile.c first_set_walk: epsilon-transition reach of RE_MATCH means the regex can match zero characters at any position; skipping bytes outside the first-byte set would skip past valid empty-match positions, so bail out instead. Imported from mruby d21eceb286.

  2. re_compile.c insert_inst: an offset equal to pos already points to the inserted instruction's new slot, so don't bump it; bumping shifts the target onto whatever code got displaced. Imported from mruby b9b8186f00.

  3. re_exec.c RE_MATCH (issue #909): pick the best match per Ruby semantics instead of letting the LAST MATCH overwrite. Earliest start position wins; within the same start, longest length (greedy) wins. Without this, "abc" =~ /\w\w/ reported the match at position 1 instead of 0 because a later-starting Pike VM thread clobbered the earlier-starting match's captures. Greedy quantifiers (\w+, scan loops) still work because the longer match at the same start wins.

Fixes #909

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
9b245eb2db6e
Parent
15b216b66030

lib/regexp/re_compile.cmodifiedView file

@@ -103,17 +103,21 @@
103103 c->code[pos].a = a;
104104 c->code[pos].offset = offset;
105105
106- /* fix all jump targets that point at or past the insertion point.
107- Issue #824: previously only JMP/SPLIT/SPLITNG offsets were
108- patched, leaving LOOKAHEAD/NEG_LOOKAHEAD/LOOKBEHIND/NEG_LOOKBEHIND
109- dangling (their offset is the jump-to-end target). */
106+ /* Fix jump targets that point past the insertion point. An offset equal
107+ to `pos` already points to the inserted instruction's new location and
108+ must not be bumped -- bumping it would shift the target to whatever
109+ code got displaced by the insertion (e.g. the body of the quantified
110+ atom), corrupting "skip past this atom" jumps emitted earlier.
111+ Imported from mruby b9b8186f00.
112+ Issue #824: also patches LOOKAHEAD/NEG_LOOKAHEAD/LOOKBEHIND/
113+ NEG_LOOKBEHIND (their offset is the jump-to-end target). */
110114 for (uint32_t i = 0; i < c->code_len; i++) {
111115 if (i == pos) continue;
112116 switch (c->code[i].op) {
113117 case RE_JMP: case RE_SPLIT: case RE_SPLITNG:
114118 case RE_LOOKAHEAD: case RE_NEG_LOOKAHEAD:
115119 case RE_LOOKBEHIND: case RE_NEG_LOOKBEHIND:
116- if (c->code[i].offset >= pos && c->code[i].offset < 0xffff) {
120+ if (c->code[i].offset > pos && c->code[i].offset < 0xffff) {
117121 c->code[i].offset++;
118122 }
119123 break;
@@ -971,12 +975,19 @@
971975 case RE_ANY: case RE_ANY_NL:
972976 return FALSE; /* any byte possible */
973977 case RE_MATCH:
974- return TRUE; /* empty match; first_bytes still valid for other branches */
978+ /* Reaching MATCH via epsilon transitions means the regex can match
979+ zero characters at any position. Skipping bytes that aren't in the
980+ first-byte set would skip past valid empty-match positions, so the
981+ optimization isn't safe -- bail out and accept any starting byte.
982+ Imported from mruby d21eceb286. */
983+ return FALSE;
975984 default:
976985 return FALSE;
977986 }
978987 }
979- return TRUE;
988+ /* Walked off the end without hitting MATCH or a consuming op. Treat as
989+ empty-matchable, same as RE_MATCH. */
990+ return FALSE;
980991 }
981992
982993 static mrb_bool