test: check __current_slot_index in snapshot#2425
Conversation
🦋 Changeset detectedLatest commit: ad8126d The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types 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 |
📝 WalkthroughWalkthroughThis PR introduces slot index tracking for multi-slot dynamic parts in the React snapshot runtime. It adds a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/react/runtime/src/snapshot/snapshot.ts (1)
376-377:⚠️ Potential issue | 🔴 CriticalGuard slot-index overflow before dereferencing
slot[index].At Line 376/377, index is incremented and dereferenced with
!without bounds checks. If insertions exceed available slots (or state drifts), this becomes a runtime crash.Suggested fix
- const index = this.__current_slot_index++; - const [s, elementIndex] = __snapshot_def.slot[index]!; + const index = this.__current_slot_index; + const slotEntry = __snapshot_def.slot[index]; + if (!slotEntry) { + throw new Error( + `Slot index overflow: index=${index}, slotCount=${__snapshot_def.slot.length}, type=${this.type}`, + ); + } + this.__current_slot_index = index + 1; + const [s, elementIndex] = slotEntry;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react/runtime/src/snapshot/snapshot.ts` around lines 376 - 377, Guard the slot access by checking that the computed index (this.__current_slot_index++) is strictly less than the available slots array length before dereferencing __snapshot_def.slot[index]; if it is out of range, either expand/allocate a new slot entry or throw a clear error (including index and __snapshot_def.slot.length) instead of using the non-null assertion (!) on slot[index]; replace the direct use of slot[index]! with a safe read that handles undefined and update the logic around __current_slot_index and __snapshot_def.slot to prevent silent runtime crashes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/react/runtime/src/snapshot/snapshot.ts`:
- Around line 83-87: The cloned snapshot can miss initialization of the new
field __current_slot_index causing undefined++ in insertBefore; update the clone
path used by takeElements so that it either copies the source's
__current_slot_index or initializes it to 0 when creating/cloning an instance.
Specifically, in the takeElements/cloning logic ensure you assign
this.__current_slot_index = source.__current_slot_index ?? 0 (or set to 0 when
no source) so insertBefore and subsequent slot lookups never operate on
undefined.
---
Outside diff comments:
In `@packages/react/runtime/src/snapshot/snapshot.ts`:
- Around line 376-377: Guard the slot access by checking that the computed index
(this.__current_slot_index++) is strictly less than the available slots array
length before dereferencing __snapshot_def.slot[index]; if it is out of range,
either expand/allocate a new slot entry or throw a clear error (including index
and __snapshot_def.slot.length) instead of using the non-null assertion (!) on
slot[index]; replace the direct use of slot[index]! with a safe read that
handles undefined and update the logic around __current_slot_index and
__snapshot_def.slot to prevent silent runtime crashes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 410252af-9f3e-4ffb-980c-826f0da8a96e
📒 Files selected for processing (3)
.changeset/few-shirts-laugh.mdpackages/react/runtime/__test__/basic.test.jsxpackages/react/runtime/src/snapshot/snapshot.ts
React External#200 Bundle Size — 591.44KiB (0%).ad8126d(current) vs 9df4844 main#197(baseline) Bundle metrics
|
| Current #200 |
Baseline #197 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
3 |
3 |
|
17 |
17 |
|
5 |
5 |
|
8.59% |
8.59% |
|
0 |
0 |
|
0 |
0 |
Bundle analysis report Branch p/test-slot-index Project dashboard
Generated by RelativeCI Documentation Report issue
Merging this PR will not alter performance
Comparing Footnotes
|
Web Explorer#8656 Bundle Size — 728.84KiB (0%).ad8126d(current) vs 9df4844 main#8653(baseline) Bundle metrics
|
| Current #8656 |
Baseline #8653 |
|
|---|---|---|
43.31KiB |
43.31KiB |
|
2.16KiB |
2.16KiB |
|
0% |
0% |
|
8 |
8 |
|
10 |
10 |
|
148 |
148 |
|
11 |
11 |
|
34.69% |
34.69% |
|
3 |
3 |
|
0 |
0 |
Bundle size by type no changes
| Current #8656 |
Baseline #8653 |
|
|---|---|---|
384.62KiB |
384.62KiB |
|
342.07KiB |
342.07KiB |
|
2.16KiB |
2.16KiB |
Bundle analysis report Branch p/test-slot-index Project dashboard
Generated by RelativeCI Documentation Report issue
React Example#7081 Bundle Size — 237.81KiB (0%).ad8126d(current) vs 9df4844 main#7078(baseline) Bundle metrics
|
| Current #7081 |
Baseline #7078 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
4 |
4 |
|
180 |
180 |
|
71 |
71 |
|
46.39% |
46.39% |
|
2 |
2 |
|
0 |
0 |
Bundle size by type no changes
| Current #7081 |
Baseline #7078 |
|
|---|---|---|
145.76KiB |
145.76KiB |
|
92.05KiB |
92.05KiB |
Bundle analysis report Branch p/test-slot-index Project dashboard
Generated by RelativeCI Documentation Report issue
React MTF Example#214 Bundle Size — 207.38KiB (0%).ad8126d(current) vs 9df4844 main#211(baseline) Bundle metrics
|
| Current #214 |
Baseline #211 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
3 |
3 |
|
174 |
174 |
|
68 |
68 |
|
46.08% |
46.08% |
|
2 |
2 |
|
0 |
0 |
Bundle size by type no changes
| Current #214 |
Baseline #211 |
|
|---|---|---|
111.23KiB |
111.23KiB |
|
96.15KiB |
96.15KiB |
Bundle analysis report Branch p/test-slot-index Project dashboard
Generated by RelativeCI Documentation Report issue
Summary by CodeRabbit
Checklist