fix(writer): owning value move 改走 doc 所有权转移 - #2
Merged
Conversation
BombaxCeiba
force-pushed
the
fix/owning-value-move
branch
2 times, most recently
from
July 19, 2026 05:53
13d7bf9 to
b483687
Compare
fix(writer): owning value move transfers doc ownership owning yyjson::value 的 operator=(value&&) 之前对 owning 和 reference 共用一套 set_value(内容覆盖 + 挂 children 保活)。但 owning 被 move 之后 val_ 还指向已经 转走的 doc 节点(悬挂)、has_parent_ 被清空,这时走 set_value 既写野指针(dst->tag) 又解引用空 shared_ptr。owning value 因此不满足 std::movable,塞进 vector 排个序 就崩。 现在 owning 的 move-assign 直接转移 doc_/val_/has_parent_ 三成员(doc 所有权转移), reference value 仍走 set_value(node 编辑语义)。get_has_parent() 顺手对空 shared_ptr 做了保护。count_children 几条断言按 doc 转移后的实际值调整(owning move-assign 不 再往 children 挂 src doc)。 新增 test_owning_value_move,覆盖 moved-from 重新赋值和 vector<value> 排序。 operator=(value&&) previously shared one set_value between owning and reference values (content overwrite + keep src doc alive via dst.children). After a move, an owning value's val_ still points into the already-transferred doc (dangling) and has_parent_ is emptied, so set_value writes a dangling pointer and dereferences an empty shared_ptr. owning value thus isn't std::movable, and sorting a vector crashes. Now owning move-assign transfers doc_/val_/has_parent_ directly; reference values keep set_value. get_has_parent() guards empty shared_ptr. count_children assertions adjusted to post-transfer values. Adds test_owning_value_move.
BombaxCeiba
force-pushed
the
fix/owning-value-move
branch
from
July 19, 2026 11:17
b483687 to
5d540c6
Compare
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.
owning value 之前的 move 会崩。operator=(value&&) 对 owning 和 reference 共用一套
set_value(内容覆盖 + 挂 children 保活),但 owning 被 move 之后 val_ 指向已经转走的
doc 节点、has_parent_ 被清空,再赋值就是写野指针 + 解引用空 shared_ptr —— 排个
vector 就炸。
改成 owning 的 move-assign 直接转 doc 所有权(doc_/val_/has_parent_),reference 保持
原样。get_has_parent 顺手挡了下空指针。count_children 几条断言按 doc 转移后的值调了
(owning move 不再往 children 挂 src doc)。
test_owning_value_move 覆盖 moved-from 重新赋值和 vector sort,本地跑过(含 Writer
suite 全过)。