Skip to content

Commit c53d6b7

Browse files
committed
Merge rust-bitcoin#2970: bip152: check if indexes do not overflow
837f466 bip152: check if indexes do not overflow (Bruno Garcia) Pull request description: When deserializing a `HeaderAndShortIds` we could check if the number of txs does not overflow 16 bits. If so, throw an error. ACKs for top commit: apoelstra: ACK 837f466 Kixunil: ACK 837f466 Tree-SHA512: 21797689758ae22666289cde123e3e9ae1bdb1ab2f85a1cd0f07ee8fa2a441e7b1b928f30b8fb1f65aef0c526c899eb96b83ed4fff074e70b31e0afad2c35930
2 parents b392510 + 837f466 commit c53d6b7

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

bitcoin/src/bip152.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,32 @@ pub struct HeaderAndShortIds {
158158
/// which we expect a peer may be missing.
159159
pub prefilled_txs: Vec<PrefilledTransaction>,
160160
}
161-
impl_consensus_encoding!(HeaderAndShortIds, header, nonce, short_ids, prefilled_txs);
161+
162+
impl Decodable for HeaderAndShortIds {
163+
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
164+
let header_short_ids = HeaderAndShortIds {
165+
header: Decodable::consensus_decode(r)?,
166+
nonce: Decodable::consensus_decode(r)?,
167+
short_ids: Decodable::consensus_decode(r)?,
168+
prefilled_txs: Decodable::consensus_decode(r)?
169+
};
170+
match header_short_ids.short_ids.len().checked_add(header_short_ids.prefilled_txs.len()) {
171+
Some(x) if x <= u16::MAX.into() => Ok(header_short_ids),
172+
_ => Err(encode::Error::ParseFailed("indexes overflowed 16 bits")),
173+
}
174+
}
175+
}
176+
177+
impl Encodable for HeaderAndShortIds {
178+
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
179+
let mut len = 0;
180+
len += self.header.consensus_encode(w)?;
181+
len += self.nonce.consensus_encode(w)?;
182+
len += self.short_ids.consensus_encode(w)?;
183+
len += self.prefilled_txs.consensus_encode(w)?;
184+
Ok(len)
185+
}
186+
}
162187

163188
impl HeaderAndShortIds {
164189
/// Creates a new [`HeaderAndShortIds`] from a full block.

0 commit comments

Comments
 (0)