Skip to content

Commit 117927b

Browse files
committed
miniscript: have a custom Node destructor
To avoid recursive calls in shared_ptr's destructor that could lead to a stack overflow.
1 parent b917c71 commit 117927b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/script/miniscript.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,23 @@ struct Node {
506506
//! The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD10).
507507
const std::vector<unsigned char> data;
508508
//! Subexpressions (for WRAP_*/AND_*/OR_*/ANDOR/THRESH)
509-
const std::vector<NodeRef<Key>> subs;
509+
mutable std::vector<NodeRef<Key>> subs;
510510
//! The Script context for this node. Either P2WSH or Tapscript.
511511
const MiniscriptContext m_script_ctx;
512512

513+
/* Destroy the shared pointers iteratively to avoid a stack-overflow due to recursive calls
514+
* to the subs' destructors. */
515+
~Node() {
516+
while (!subs.empty()) {
517+
auto node = std::move(subs.back());
518+
subs.pop_back();
519+
while (!node->subs.empty()) {
520+
subs.push_back(std::move(node->subs.back()));
521+
node->subs.pop_back();
522+
}
523+
}
524+
}
525+
513526
private:
514527
//! Cached ops counts.
515528
const internal::Ops ops;

0 commit comments

Comments
 (0)