Skip to content

Commit c1ecd13

Browse files
committed
miniscript: check validity when decoding a Script
1 parent 484ec33 commit c1ecd13

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

bitcoin/script/miniscript.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ inline NodeRef<Key> DecodeScript(I& in, I last, const Ctx& ctx) {
10891089
to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
10901090

10911091
while (!to_parse.empty()) {
1092+
// Exit early if the Miniscript is not going to be valid.
1093+
if (!constructed.empty() && !constructed.back()->IsValid()) return {};
10921094

10931095
// Get the current context we are decoding within
10941096
auto [cur_context, n, k] = to_parse.back();
@@ -1408,7 +1410,11 @@ inline NodeRef<Key> DecodeScript(I& in, I last, const Ctx& ctx) {
14081410
}
14091411
}
14101412
if (constructed.size() != 1) return {};
1411-
return constructed.front();
1413+
const NodeRef<Key> tl_node = std::move(constructed.front());
1414+
// Note that due to how ComputeType works (only assign the type to the node if the
1415+
// subs' types are valid) this would fail if any node of tree is badly typed.
1416+
if (!tl_node->IsValidTopLevel()) return {};
1417+
return tl_node;
14121418
}
14131419

14141420
} // namespace internal

0 commit comments

Comments
 (0)