Skip to content

Commit 8a6c0c0

Browse files
committed
Validate handle_blockchain_block_headers() arguments.
1 parent c536b8a commit 8a6c0c0

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

src/protocols/protocol_electrum.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ bool protocol_electrum::handle_event(const code&, node::chase event_,
114114
return true;
115115
}
116116

117+
// Utility.
118+
// ----------------------------------------------------------------------------
119+
120+
// TODO: move to system/math.
121+
template <typename Integer, if_integer<Integer> = true>
122+
bool to_integer(Integer& out, double value) NOEXCEPT
123+
{
124+
if (!std::isfinite(value))
125+
return false;
126+
127+
double integral{};
128+
const double fractional = std::modf(value, &integral);
129+
if (fractional != 0.0)
130+
return false;
131+
132+
if (integral > static_cast<double>(system::maximum<Integer>) ||
133+
integral < static_cast<double>(system::minimum<Integer>))
134+
return false;
135+
136+
BC_PUSH_WARNING(NO_STATIC_CAST)
137+
out = static_cast<Integer>(integral);
138+
BC_POP_WARNING()
139+
return true;
140+
}
141+
117142
// Handlers (blockchain).
118143
// ----------------------------------------------------------------------------
119144

@@ -127,10 +152,23 @@ void protocol_electrum::handle_blockchain_block_header(const code& ec,
127152

128153
// electrum-protocol.readthedocs.io/en/latest/protocol-basics.html#block-headers
129154
void protocol_electrum::handle_blockchain_block_headers(const code& ec,
130-
rpc_interface::blockchain_block_headers, double ,
131-
double , double ) NOEXCEPT
155+
rpc_interface::blockchain_block_headers, double start_height, double count,
156+
double cp_height) NOEXCEPT
132157
{
133-
if (stopped(ec)) return;
158+
if (stopped(ec))
159+
return;
160+
161+
size_t quantity{};
162+
size_t waypoint{};
163+
size_t starting{};
164+
if (!to_integer(quantity, count) ||
165+
!to_integer(waypoint, cp_height) ||
166+
!to_integer(starting, start_height))
167+
{
168+
send_code(error::invalid_argument);
169+
return;
170+
}
171+
134172
send_code(error::not_implemented);
135173
}
136174

0 commit comments

Comments
 (0)