fix: prevent panic when merging a notetype with more fields#5070
Open
krMaynard wants to merge 1 commit into
Open
fix: prevent panic when merging a notetype with more fields#5070krMaynard wants to merge 1 commit into
krMaynard wants to merge 1 commit into
Conversation
merge_fields and merge_templates used the incoming notetype's index directly as a position into our own fields/templates. When the incoming notetype is longer than ours — e.g. a corrupt notetype whose field matches one of ours a second time at an out-of-bounds index — the swap/insert panicked with "index out of bounds", making the deck impossible to import. Clamp the target index to the valid range so the merge degrades gracefully instead of panicking, and add regression tests covering a longer `other` with a duplicate field and a duplicate template. Fixes ankitects#4345.
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.
Linked issue (required)
Fixes #4345.
Summary / motivation (required)
Notetype::merge_fieldsandmerge_templates(rslib/src/notetype/merge.rs) used the incoming notetype's index directly as a position into our ownfields/templates. When the incoming notetype has more fields than ours — e.g. a corrupt notetype whose field matches one of ours a second time at an index past the end of our list —Vec::swap/Vec::insertreceived an out-of-bounds index and panicked:This made the affected deck impossible to import.
The fix clamps the target index to the valid range so the merge degrades gracefully instead of panicking. All incoming fields/templates are still merged in; a duplicate match simply collapses onto the existing entry rather than crashing.
Steps to reproduce (required, use N/A if not applicable)
index out of boundsinnotetype/merge.rs.How to test (required)
Checklist (minimum)
./ninja checkor an equivalent relevant check locally.Details
Added two regression tests —
merge_longer_other_with_duplicate_fieldandmerge_longer_other_with_duplicate_template. Verified they panic on the unfixed code and pass with the fix, and that the wholenotetype::mergemodule passes:rustfmt --checkis clean on the changed file.Before / after behavior (optional)
Before: importing such a notetype panics and aborts the import. After: the merge completes, mapping the duplicate onto the existing field/template.
Risk / compatibility / migration (optional)
Low — the clamp only changes behavior in the previously-panicking out-of-bounds case; in-range merges are unaffected.
UI evidence (required for visual changes; otherwise N/A)
N/A
Scope