Skip to content

Commit

Permalink
miniscript: have a custom Node destructor
Browse files Browse the repository at this point in the history
To avoid recursive calls in shared_ptr's destructor that could lead to a
stack overflow.
  • Loading branch information
darosior committed Mar 16, 2023
1 parent b24f946 commit b4e747e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/script/miniscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,20 @@ struct Node {
//! The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD10).
const std::vector<unsigned char> data;
//! Subexpressions (for WRAP_*/AND_*/OR_*/ANDOR/THRESH)
const std::vector<NodeRef<Key>> subs;
mutable std::vector<NodeRef<Key>> subs;

/* Destroy the shared pointers iteratively to avoid a stack-overflow due to recursive calls
* to the subs' destructors. */
~Node() {
while (!subs.empty()) {
auto node = std::move(subs.back());
subs.pop_back();
while (!node->subs.empty()) {
subs.push_back(std::move(node->subs.back()));
node->subs.pop_back();
}
}
}

private:
//! Cached ops counts.
Expand Down

0 comments on commit b4e747e

Please sign in to comment.