Skip to content

reset iter->pre after removing an element from an iterator - #293

Merged
ibireme merged 1 commit into
ibireme:masterfrom
dxbjavid:iter-remove-stale-pre
Jul 22, 2026
Merged

reset iter->pre after removing an element from an iterator#293
ibireme merged 1 commit into
ibireme:masterfrom
dxbjavid:iter-remove-stale-pre

Conversation

@dxbjavid

Copy link
Copy Markdown
Contributor

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.

@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (ac8f607) to head (5c8284f).

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           
Flag Coverage Δ
unittests 98.47% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ibireme
ibireme merged commit 9365ddc into ibireme:master Jul 22, 2026
50 checks passed
@dxbjavid

Copy link
Copy Markdown
Contributor Author

TY!

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.

2 participants