Fix shared regex state across repeated matcher use (#749) - #834
Open
chiliec wants to merge 1 commit into
Open
Conversation
evaluateRegex created a single RegExp and closed over it, so a regex bound to a variable (evaluated once) reused the same stateful RegExp — and its mutable lastIndex — across every $match/$replace/$split call. Independent calls then interfered through the shared lastIndex, so the same input produced different output on repeated use, breaking JSONata's immutability guarantee (a regression between 2.0.1 and 2.0.6). Create a fresh RegExp for each top-level match walk, and thread that walk's own instance through the next() chain so multi-match iteration is unchanged. Adds a regression test (regex/case039) covering the repeated-use scenario from the issue. Full suite passes with 100% coverage maintained. Fixes jsonata-js#749. Signed-off-by: Vladimir Babin <vovababin@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #749.
Problem
A regex bound to a variable and reused across multiple
$match/$replace/$splitcalls produces different results on repeated use, breaking JSONata's immutability guarantee. It worked in 1.8.7 / 2.0.1 and regressed by 2.0.6.Expected every element to be
"a|b|c". Onmasterit returns corrupted values such as"a|b|,b,|,|b,c".Root cause
evaluateRegexbuilds oneRegExpand closes over it:A regex literal bound to a variable is evaluated once, so the returned closure — and that single stateful
rewith its mutablelastIndex— is shared by every subsequent matcher call.$replacewalks matches via thenext()chain, which depends onre.lastIndexpersisting; when another call reuses the same closure it resets/advances the samelastIndex, so independent calls interfere and corrupt each other's results.Fix
Create a fresh
RegExpfor each top-level match walk (when the closure is entered without an existing instance), and thread that walk's own instance through thenext()chain so multi-match iteration within a single walk is unchanged. Independent calls no longer share mutable regex state.Test
Adds
test/test-suite/groups/regex/case039.json, the repeated-use scenario from the issue (asserts every$replaceyields"a|b|c").Verification (local, Node 20.18.1)
case039fails (1799 passing / 1 failing); with the fix it passes (1800 passing / 0 failing).npm test— full suite green, and the strict gate holds: Statements/Branches/Functions/Lines all 100% (nyc check-coverage --branches 100exits 0). The new branch is covered bycase039.Signed-off-by: Vladimir Babin vovababin@gmail.com