]> git.kaiwu.me - njs.git/commit
RegExp: fix Symbol.replace() with an inconsistent match object
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 2 Sep 2026 23:06:22 +0000 (16:06 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Fri, 4 Sep 2026 20:54:20 +0000 (13:54 -0700)
commit647b0e28643e2593a470173eb017664e429712df
treefe5085d7cba15e722c63b3ef851d007810de558e
parent9aab9de86726a930e41450f44f685a532a40db1c
RegExp: fix Symbol.replace() with an inconsistent match object

Previously, the matched string returned by exec() was assumed to be a
substring of the subject at the reported index.
njs_string_get_substitution() converted "index + matched length" to a byte
offset without a bounds check, and the replace loop advanced the end of the
last match by the byte size of the matched string instead of its character
length.  As a result the $' expansion read outside the subject, and on a
multibyte subject the result could start in the middle of a UTF-8 sequence.

The fix is to pass the head and tail slices of the subject to
njs_string_get_substitution() and to track the end of the last match as a
character index.  The function no longer converts indexes to offsets, so the
out of range conversion is prevented by construction.

This is an extended fix for #1116 pull request on Github.

Reported by Basavaraj S m (basavaraj@digiscrypt.com).
src/njs_regexp.c
src/njs_string.c
src/njs_string.h
src/test/njs_unit_test.c