reset iter->pre after removing an element from an iterator - #293
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #293 +/- ##
=======================================
Coverage 98.47% 98.47%
=======================================
Files 2 2
Lines 7739 7743 +4
=======================================
+ Hits 7621 7625 +4
Misses 118 118
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
TY! |
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.
yyjson_mut_arr_iter_remove and yyjson_mut_obj_iter_remove rewind iter->cur to the removed element's predecessor but leave iter->pre pointing at that same node, so pre is no longer the previous value once a remove has happened. Since pre is only read by these two functions, the state survives unnoticed until remove is called a second time without an intervening next(): prev and cur are then the same node, the relink becomes a no-op, but idx, max and the container length are still decremented and the tail pointer may be overwritten.
The effect is that the second call hands back an element which is still linked into the container while the stored length no longer matches the ring, so a caller that inserts the returned value somewhere else quietly ends up with the same node under two parents. On a three element array a next/next/remove/remove sequence returns element 0 and leaves the array reporting size 1 whilst still serialising that element; the object iterator behaves the same way.
This clears pre after unlinking and requires it in the guard, which matches what yyjson_ptr_ctx_remove already does in the same header, so a repeated remove returns NULL rather than corrupting the container. Regression tests for both iterators are included and fail without the change.