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>
|
Thanks for raising this, I agree it is a bug. I don't quite agree on the root cause though. This same code works in the v1.x branch but has regressed in the V2.x (master) branch. This suggests the problem is caused by the lazy evaluation of the async/await mechanism in v2 vs generator pattern used in v1. A simpler fix (tested) is to store the I.e.: var lastIndex = re.lastIndex;
result.next = function() {
if(lastIndex >= str.length) {
return undefined;
} else {
var next = closure(str, lastIndex);
....
return next;
}
};Although your proposed fix works, it would cause a significant performance regression, especially if the regex guardrail hook was being used to perform static (ReDoS) analysis of the regex prior to compilation. Happy for you to use the above code in your PR, or if you prefer I'll put this fix in a separate PR. Many thanks :) |
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