Commit 4f675b40

4f675b400716Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(regexp): SAVE bounds-checks slot against ncap (#779)

The Pike VM's RE_SAVE handler wrote inst.offset into the per-thread capture row without bounds-checking against s->ncap. A backref to a non-existent group (/([a-z]+).*\\2/) caused the SAVE for the phantom slot to write past the row, corrupting adjacent thread data and producing a false-negative match.

The backtracking VM at line 495 already had the same guard; this commit brings the Pike VM in line.

Closes #779

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

Details

Commit
4f675b400716
Parent
5f036ed0f319

lib/regexp/re_exec.cmodifiedView file

@@ -144,7 +144,10 @@
144144 continue;
145145
146146 case RE_SAVE:
147- if (!s->match_only) {
147+ /* Issue #779: bounds-check the slot offset against ncap so a
148+ SAVE instruction emitted for a non-existent capture group
149+ doesn't write past the per-thread capture row. */
150+ if (!s->match_only && inst.offset < s->ncap) {
148151 CAP(s, cap_slot)[inst.offset] = (int)(sp - s->str);
149152 }
150153 pc++;