Skip to content

Commit c343af6

Browse files
committed
descriptor: Add proper Clone function to miniscript::Node
Multipath descriptors requires performing a deep copy, so a Clone function that does that is added to miniscript::Node instead of the current shallow copy.
1 parent df3f63c commit c343af6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/script/descriptor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ class MiniscriptDescriptor final : public DescriptorImpl
13601360
for (const auto& arg : m_pubkey_args) {
13611361
providers.push_back(arg->Clone());
13621362
}
1363-
return std::make_unique<MiniscriptDescriptor>(std::move(providers), miniscript::MakeNodeRef<uint32_t>(*m_node));
1363+
return std::make_unique<MiniscriptDescriptor>(std::move(providers), miniscript::MakeNodeRef<uint32_t>(m_node->Clone()));
13641364
}
13651365
};
13661366

src/script/miniscript.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,20 @@ struct Node {
523523
}
524524
}
525525

526+
Node<Key> Clone() const
527+
{
528+
// Use TreeEval() to avoid a stack-overflow due to recursion
529+
auto upfn = [](const Node& node, Span<Node> subs) {
530+
Node<Key> ret(node);
531+
ret.subs.clear();
532+
for (const auto& sub : subs) {
533+
ret.subs.push_back(MakeNodeRef<Key>(sub));
534+
}
535+
return ret;
536+
};
537+
return TreeEval<Node<Key>>(upfn);
538+
}
539+
526540
private:
527541
//! Cached ops counts.
528542
const internal::Ops ops;
@@ -630,7 +644,10 @@ struct Node {
630644
// If evaluation returns std::nullopt, abort immediately.
631645
if (!result) return {};
632646
// Replace the last node.subs.size() elements of results with the new result.
633-
results.erase(results.end() - node.subs.size(), results.end());
647+
// Use pop_back to truncate results to avoid MoveAssignable requirement of erase().
648+
for (size_t i = 0; i < node.subs.size(); ++i) {
649+
results.pop_back();
650+
}
634651
results.push_back(std::move(*result));
635652
stack.pop_back();
636653
}

0 commit comments

Comments
 (0)