Fix a scalar set() through a view of a value that an edit dropped
set(target, scalar) on an array/object that spans more than one node first switches the container that target is in to links, since that container steps over target's parsed subtree by its node count. The container was looked up from root(); for a target that is no longer part of the document (an edit erased or replaced it, or a value that contains it) the lookup found nothing and the scalar was written anyway. The dropped container then read past its nodes when walked, and a later edit of it made block_of() write more links than the block it allocated holds. When the lookup from root() fails, look for the container whose contiguous layout still steps over the slot: the nearest container before it in its allocation whose extent covers it (edits never enlarge an extent, so this is the original parent). If that container still uses its layout, it switches to links as a live parent does. Every other edit through a view of a dropped value already worked and changed only the detached value; the scalar set now does the same, as documented. Also stop block_of() when its block is full, so that it never writes past the capacity it allocated. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
6 files changed
+181
-8
No files matched your search
Generated
+53
-3
@@ -5564,7 +5564,9 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
|
||||
h->len = n->len;
|
||||
h->next = static_cast<std::uint32_t>(used);
|
||||
node* o = h + 1;
|
||||
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;)
|
||||
// (the bound on o keeps the copy inside the block even if the elements
|
||||
// disagreed with the element count)
|
||||
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n), *const full = h + used; c != e && o != full;)
|
||||
{
|
||||
if (object)
|
||||
{
|
||||
@@ -5646,6 +5648,41 @@ inline node* find_parent(const document_data& d, const node* target)
|
||||
}
|
||||
}
|
||||
|
||||
/// The container whose contiguous layout (in the parsed index, or in a new
|
||||
/// value) steps over `target`, also when an edit dropped the container or a
|
||||
/// value that encloses it; nullptr if there is none, or if the container no
|
||||
/// longer walks that layout (its elements moved). An edit never gives a node a
|
||||
/// larger extent than it was parsed or encoded with (a scalar is one node, an
|
||||
/// assigned array/object keeps the extent of the slot), so the containers whose
|
||||
/// extent covers `target` are its original ancestors: the nearest one before it
|
||||
/// is its original parent. The root of a new value that is the element sequence
|
||||
/// of another container is walked by that container (which find_parent() finds
|
||||
/// while it still uses the sequence), not by itself.
|
||||
inline node* find_layout_parent(const document_data& d, const node* target)
|
||||
{
|
||||
const std::less<const node*> lt{};
|
||||
const node* lo = d.nodes;
|
||||
if (lt(target, lo) || !lt(target, d.nodes + d.node_count))
|
||||
{
|
||||
auto it = d.edits->regions.upper_bound(target);
|
||||
if (it == d.edits->regions.begin())
|
||||
{
|
||||
return nullptr; // LCOV_EXCL_LINE (an array/object with elements is in the index or a new value)
|
||||
}
|
||||
--it;
|
||||
lo = it->second != nullptr ? it->first + 1 : it->first;
|
||||
}
|
||||
for (const node* c = target; c != lo;)
|
||||
{
|
||||
--c;
|
||||
if (is_container(*c) && lt(target, c + c->next))
|
||||
{
|
||||
return (c->flags & node_flags::moved) == 0 ? const_cast<node*>(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -6522,9 +6559,22 @@ class editor
|
||||
// for the container is linear in the size of the document, so
|
||||
// links (which are marked in the slot) avoid it.
|
||||
node* const p = parent_known ? parent : find_parent(m_doc, slot);
|
||||
if (p != nullptr && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0))
|
||||
if (p != nullptr)
|
||||
{
|
||||
block_of(m_doc, p, 0);
|
||||
if ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)
|
||||
{
|
||||
block_of(m_doc, p, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// the slot is no longer part of the document, but the
|
||||
// (dropped) container it is in may still step over it
|
||||
node* const q = find_layout_parent(m_doc, slot);
|
||||
if (q != nullptr)
|
||||
{
|
||||
block_of(m_doc, q, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
*slot = e.scalar;
|
||||
|
||||
Reference in new issue
Block a user