Skip to content

Fix crash when moving a zero-length range#307

Open
sarathfrancis90 wants to merge 1 commit into
Rich-Harris:masterfrom
sarathfrancis90:fix-move-zero-length-range
Open

Fix crash when moving a zero-length range#307
sarathfrancis90 wants to merge 1 commit into
Rich-Harris:masterfrom
sarathfrancis90:fix-move-zero-length-range

Conversation

@sarathfrancis90

Copy link
Copy Markdown

move() crashes with an opaque TypeError: Cannot read properties of undefined (reading 'next') when called with a zero-length range (start === end):

new MagicString('abcdefghijkl').move(0, 0, 6);

The reason is that for an empty range byEnd[end] has no chunk, so last is undefined and the subsequent last.next blows up. remove() already short-circuits a zero-length range as a no-op (if (start === end) return this;), so I did the same in move() — moving nothing has no effect.

I found this while fuzzing random valid operation sequences. Added a test alongside the other move edge cases; full suite passes.

move(start, end, index) threw a TypeError ('Cannot read properties of
undefined') when start === end, because byEnd[end] is undefined for an
empty range so reading last.next failed. remove() already treats a
zero-length range as a no-op; do the same in move().
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.

1 participant