Repository navigation
fix(muya): regex search freezes on zero-width patterns and splits emoji - #5333
Merged
Merged
Conversation
`matchString` looped over `execall`, which keeps calling `exec` until it returns null. A zero-width match leaves a global RegExp's `lastIndex` where it was, so a pattern such as `\b` or `(?=b)` returned the same empty match forever. The desktop find bar only rejects patterns that match the empty string, so enabling regex and searching `\b` or `(?=b)` froze the editor. Collect matches with our own loop that bumps `lastIndex` after an empty match. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
A user regexp was compiled without the Unicode flag, so `.`, `[^a-z]` and friends matched single UTF-16 code units. With the regex toggle on, finding `[^a-z]` in `abc🙂` matched each half of the emoji, and "Replace" left a lone surrogate in the block text: the next state flush threw "Invalid offset - splits unicode bytes", the same crash as #4926. Compile user patterns with the `u` flag so they match whole code points, falling back to the plain flags for patterns that are only valid without it (such as `\-`). In that fallback, drop matches that start or end inside a surrogate pair, and step a zero-width match over a whole pair in Unicode mode so `exec` does not resume on it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
`matchString` now collects matches with its own `exec` loop, so @muyajs/core no longer imports `execall`. The package stays in the lockfile for the desktop and legacy muyajs packages. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
Nothing in the desktop package imports `execall`; only a leftover module shim declared it. The legacy muyajs package still imports it and keeps its own dependency. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
Contributor
|
Website preview ready: https://pr-5333-marktext-website.ransixi.workers.dev Built from |
Regex search matched code points, so `.` or `[^a-z]` matched each of the seven code points of `🧑🧑🧒🧒` and Replace broke the emoji apart; a literal search for `🧑` also matched inside it. A grapheme cluster (an emoji ZWJ sequence, a letter with combining marks) is one character on screen, so align matches to cluster boundaries: a literal match covering only part of a cluster is dropped, a regexp match reaching into one expands to the whole cluster (overlapping expansions merge), and a zero-width match inside a cluster is dropped. This also covers the lone surrogate case the previous filter handled. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
This was referenced Sep 14, 2026
Closed
Closed
Closed
Closed
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.
Related to #4926 (same crash, different entry point). Found while verifying #4931 / #5332; no existing issue.
Summary
Search and replace bugs in
matchString(packages/muya/src/utils/search.ts), all reachable from the desktop find bar: a freeze and an emoji-splitting crash with the regex toggle on, and replace breaking multi-code-point characters apart. Plus two cleanup commits that drop theexecalldependency the first fix made unused.1. Zero-width patterns freeze the editor
matchStringcollected matches withexecall, which callsexecuntil it returnsnull. A zero-width match leaves a global RegExp'slastIndexwhere it was, soexecreturns the same empty match forever.The desktop find bar already refuses patterns that match the empty string (
RegExp(value).test('')), which covers^,$andx*, but a zero-width pattern that doesn't match''still goes through:abc/xyz)a\b(?=b)Fix: our own
execloop that stepslastIndexpast an empty match.2.
./[^…]split emoji, and Replace crashesUser regexps were compiled without the
uflag, so.and character classes match single UTF-16 code units. Finding[^a-z]inabc🙂matched each half of the emoji; Replace then left a lone surrogate (abcx\ude42), and the next state flush threwInvalid offset - splits unicode bytes— the #4926 crash through a different door. In real Chrome on develop (with #4931):.abcx\ude42, crashabcx[^a-z]abcx\ude42, crashabcxc.abC\ude42, crashabC.xxxxx(5 for 4 characters)xxxx🙂abcxabcxFix:
uflag so they match whole code points.\-are a SyntaxError in Unicode mode), so existing patterns keep working.lastIndexby one would loop again).3–4. Drop the unused
execalldependencychore(muya):@muyajs/coreno longer importsexecall; remove it frompackages/muya/package.json.chore(desktop): nothing in the desktop package imports it either (only a leftoverdeclare module 'execall'shim); remove the dependency and the shim.packages/muyajs(legacy engine) still importsexecalland keeps its own dependency, so the package stays in the lockfile; each commit only removes that importer's 3-line entry frompnpm-lock.yaml(pnpm install --lockfile-onlyproduces the identical file).5. A grapheme cluster is replaced as one unit
Even with
u, a regexp matches code points, and🧑🧑🧒🧒is one character on screen but 7 code points (four people joined by ZWJs)..or[^a-z]matched each piece and Replace broke the emoji apart; a literal search for🧑also matched inside it.Matches are now aligned to grapheme cluster boundaries (
Intl.Segmenter, via thegraphemeClustershelper from #4931):🧑wipe out the whole family).On
🧑🧑🧒🧒zin Chromium and WebKit:., replace all withxxxxxxxxxxx[^a-z], replace onex🧑🧒🧒zxz🧑🧑🧑🧒🧒/zSegmentation only runs for a block that has matches and contains a character at or above U+0300 (or a CR), so plain ASCII search is unaffected. This alignment also subsumes the lone-surrogate filter from commit 2.
Type of change
Test plan
utils/__tests__/search.spec.ts: zero-width patterns (\b,(?=b), an empty alternative inside an emoji), surrogate pairs (.,[^a-z],c.), the\-fallback, and grapheme clusters (regex./[^a-z]/.{2}on a ZWJ family, a base letter with a combining mark, a zero-width match inside a cluster, literal partial matches dropped, literal whole cluster found).RegExp#execis capped at 1000 calls so a looping regression fails instead of hanging the runner. Every new case except the\-and whole-cluster controls fails without its fix.search/__tests__/search.spec.ts:search('[^a-z]')+replace('x')onabc🙂yieldsabcx; regex.replace-all on🧑🧑🧒🧒zyieldsxx; a literal🧑search inside the family finds nothing.vitest run224 files / 1570 tests pass;lint(no new warnings),lint:types,check-circularpass.^,\b,x*,q?,(?=b)searches finish in a few hundredexeccalls (develop: unbounded).build:unpack+ Playwright/Electron): the find-bar freeze table above, plus regex Replace of[^a-z]inabc🙂→abcxwith no renderer errors (develop:abcx\ude42+Invalid offset - splits unicode bytes).Notes for reviewers [optional]
execallcommits:pnpm install --frozen-lockfile, desktoptypecheck/build:unpack/validate-licensespass.THIRD-PARTY-LICENSES.txtis not touched: regenerating it still listsexecall(bundled via@marktext/muyajs), and the checked-in file is already out of date with unrelated entries, so a regeneration belongs in its own change.u, case-insensitive regex search uses Unicode case folding (e.g./k/iualso matches the Kelvin signK). That's the intended semantics for a Unicode-aware search.eno longer matches theeofe+ U+0301, while regexematches the wholeé.By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.
🤖 Generated with Claude Code
https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn