Repository navigation
fix: reject imported Text ops past the end of the text - #1169
Merged
Merged
Conversation
An imported Text insert/delete/mark past the end of the text panicked in `insert_elem_at_entity_index` with the doc locks held (a trap in WASM). `RichtextState::validate_diff` now bounds-checks the delta like List/MovableList, and Text ops now open the import rollback scope so the rejected import leaves the doc unchanged and usable. Rationale and cost: context/movable-list-op-validation.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
WASM Size Report
|
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.
Summary
Importing a Text insert, delete or mark whose position is past the end of the text panicked at
richtext_state.rs:1653with the doc locks held (the doc was poisoned; in WASM the instance trapped withRuntimeError: unreachable). Such imports now returnErr(DecodeError)and leave the document unchanged and usable, the same way out-of-bounds List/MovableList ops are handled (#1125).Fixes #1160
Changes
RichtextState::validate_diff: bounds-checks the text diff in entity-index space (style anchors count), mirroringListState::validate_diff/MovableListState::validate_diff. It runs inDocState::validate_diff_batchbefore any container state is mutated.oplog::state_apply_can_reject(new, shared byOpLog::preflight_import_changesandPendingChanges::has_state_apply_rollback_ops): Text ops now open the import rollback scope, like List/MovableList/Tree. Without the scope, avalidate_differror hitspanic!("state apply returned Err for import without rollback guard").context/movable-list-op-validation.md(Text check, rollback-scope list, cost), pointer incrates/loro-internal/src/state/AGENTS.md.loro-crdt(patch).Rationale
Out-of-range positions are invalid external input, so they are rejected with
Err, not a panic. The check runs on the diff that is about to be applied to state, so it covers the linear fast path and the tracker (concurrent/checkout) path the same way. Wrong positions are shifted by concurrent inserts but still overrun, e.g.retains 5 items but state only has 4. The rollback scope undoes the op log, including pending changes unlocked by the import and wholeimport_batchbatches (BatchImportGuard::finish).Validation
crates/loro/tests/text_invalid_ops.rs(6 tests): the exact Importing a Text insert past the end of the text panics (richtext_state.rs:1653) #1160 repro; insert/delete (forward and reversed)/mark past the end, including an op that is only out of bounds after an earlier op of the same change; on a plain doc and on one with a concurrent edit; throughimport_json_updates, binaryimportandimport_batch(stays attached); a forged op depending on a map-only change in the same import; a parked forged op unlocked by a later map-only import (a mutation check showed this fails without the pending-scan change); in-bounds controls still import. On main, the first test aborts the test binary with the Importing a Text insert past the end of the text panics (richtext_state.rs:1653) #1160 panic.crates/loro-wasm/tests/text_invalid_ops.test.ts: fails on main withRuntimeError: unreachable, passes here.cargo test -p loro -p loro-internal: 1328 passed, 0 failed.pnpm check(clippy-Dwarnings): clean.base64.test.ts, which needs the base64 target that a dev build does not produce.Performance
Opening the rollback scope costs a few small clones and journal entries per import. Release build, ad-hoc bench (not committed), machine load avg 4–15, alternating main/PR runs, min of 7 rounds:
That is about +0.3 µs per tiny import (≈ +12% on this microbench). Large imports are not measurably slower. List/MovableList/Tree imports already pay this cost.
Follow-ups (not fixed here)
markwithstart >= end(e.g.start: 2, end: 2) hitsdebug_assert!(start < end)indiff_calc.rs(text linear diff). It is a different class from out-of-bounds positions. Amarkwithout itsmark_end(or the reverse) was not examined.AppDag::begin_import_rollbackclones frontiers, vv and shallow fields eagerly. Making this lazy would cut the per-import overhead above for every sequence/tree container type.attach()/checkout_to_latest, because theyexpectthe checkout. This already applied to List/MovableList (seecontext/movable-list-op-validation.md, "Known gaps").🤖 Generated with Claude Code