Skip to content

Prevent stack overflow in isWordCharacter on consecutive chameleon characters - #6366

Open
Matoo125 wants to merge 1 commit into
ianstormtaylor:mainfrom
Matoo125:fix-iswordcharacter-stack-overflow
Open

Matoo125 wants to merge 1 commit into
ianstormtaylor:mainfrom
Matoo125:fix-iswordcharacter-stack-overflow

Conversation

@Matoo125

Copy link
Copy Markdown

Description
When encountering a chameleon character (e.g. ', \u2018, \u2019), isWordCharacter in packages/slate/src/utils/string.ts recursively inspected following characters to determine if they form part of a word. A long sequence of consecutive quote characters caused unbounded recursion leading to a V8 call stack overflow (RangeError: Maximum call stack size exceeded).

This pull request bounds the recursion depth when checking consecutive chameleon characters and ensures empty character inputs evaluate to false.

Issue
N/A

Example

import { getWordDistance } from 'slate/src/utils/string'

// Previously threw: RangeError: Maximum call stack size exceeded
// Now evaluates safely without stack overflow:
getWordDistance("'"'.repeat(15000)) // => 15000

Context
Apostrophes inside words (such as contractions) are isolated single characters between word letters. Multiple consecutive quotation characters do not form word-internal joiners under standard Unicode word boundary rules (UAX #29). Limiting the lookahead recursion depth for chameleon sequences prevents stack exhaustion while preserving word-distance behavior for legitimate words.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

The CHAMELEON regex matches apostrophes (' \u2018 \u2019). When an apostrophe
is encountered, isWordCharacter recursed on the remaining string to check
whether subsequent characters are part of a word. A long sequence of
consecutive quotes caused unbounded recursion leading to a V8 call stack
exhaustion (RangeError: Maximum call stack size exceeded).

Bound the recursion depth when inspecting chameleon characters and ensure
empty characters evaluate to false.

Reported-by: BS.ehlas.cz
@changeset-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 733eeb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

// Chameleons count as word characters as long as they're in a word, so
// recurse to see if the next one is a word character or not.
if (CHAMELEON.test(char)) {
if (depth > 0 || !remaining) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this changes word results, not just the overflow case
  • getWordDistance("'hi'") goes from 4 to 3, a quote at the end of the text no longer joins the word
  • getWordBounds("''a", 0) goes from [0, 3] to [0, 0], the second quote now stops the lookahead
  • Editor.positions word steps and Editor.word both read these, so word delete and word moves change near quotes
  • I compared main and this branch over every string up to 6 chars of a ' ’ space ., about 28k results differ
  • a loop that skips the quotes keeps main's results exactly (0 differences on 1.2M cases) and has no recursion:
while (CHAMELEON.test(char)) {
  const charDist = getCharacterDistance(remaining, isRTL)
  ;[char, remaining] = splitByCharacterDistance(remaining, charDist, isRTL)
}

return !SPACE.test(char) && !PUNCTUATION.test(char)
  • that also handles the 15000 quote test, and depth and the !char check can go

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants