Skip to content

fix: avoid hangs when slicing empty strings - #392

Open
nithishreddy5771 wants to merge 3 commits into
Rich-Harris:masterfrom
nithishreddy5771:fix/empty-slice-hang
Open

nithishreddy5771 wants to merge 3 commits into
Rich-Harris:masterfrom
nithishreddy5771:fix/empty-slice-hang

Conversation

@nithishreddy5771

Copy link
Copy Markdown

Summary

On current master, new MagicString('').slice(-1), .slice(1, 2) and .slice(-2, 1) never return. The empty original has a zero-width chunk at index 0. _chunkAt follows byStart or byEnd back to that same chunk when looking outside the original range.

Return undefined before walking for indices below 0 or strictly above original.length. Keep index equal to the length eligible for lookup: the empty-original boundary can contain inserted content, and its existing slice behavior is preserved. This also leaves the second caller, _hasRemovedContent, unchanged for valid match positions.

Add seven regression/control cases covering 168 empty-original slice calls across negative, zero and positive offsets, explicit and default ends, boundary insertions and integer validation. The regression cases cap map reads so the original bug fails a test rather than hanging the runner.

Validation

All checks used Linux x86_64, Node 22.23.5 and pnpm 12.3.4 with the frozen lockfile. Separate automated validation used the same runtime and OS, not another platform.

  • Untouched native suite: 494 tests pass in 41 files. With this patch: 501 pass in 41 files. Build and publint pass.
  • Added tests on original source: 3 failures and 23 passes in the full slice file. With the fix: all 26 slice tests pass.
  • The three affected calls each hit an 800ms child-process timeout before the fix and return the expected empty string afterward; three ordinary controls pass before and after.
  • Additional full runs with --sequence.shuffle.files --sequence.seed=1 --maxWorkers=1 pass 494 baseline / 501 patched tests. Shuffled run orders were not asserted identical.
  • Full ESLint, TypeScript checks and git diff --check pass.

There is a pre-existing intermittent legacy storeName warning assertion failure in update.test.ts. Separate validation reproduced it in 2 of 8 runs on untouched original source and 2 of 8 on this candidate. Successful complete runs are reported above, not a claim that every run passed. This patch does not change the warning tests; #388 addresses their shared-state ordering. That PR also edits nearby lines in the slice test file, so integration may need a textual rebase if it lands first.

Other Node versions, Windows/macOS, performance and downstream consumers were not tested. No production incident or coverage percentage is claimed. This is separate from #384's empty-match replacement fix.

Implemented and tested by an AI assistant on my behalf. The separate check was automated; no human code review is claimed.

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