From 85a762d2dee049b0392aa0e6e1007d5ea0024ea1 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Tue, 21 Nov 2023 14:07:23 +0000 Subject: [PATCH] Fix a bug in on_have --- crates/librqbit/src/torrent_state.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/librqbit/src/torrent_state.rs b/crates/librqbit/src/torrent_state.rs index 34f672c..c0a84d3 100644 --- a/crates/librqbit/src/torrent_state.rs +++ b/crates/librqbit/src/torrent_state.rs @@ -1045,7 +1045,18 @@ impl PeerHandler { self.state .peers .with_live_mut(self.addr, "on_have", |live| { - live.bitfield.set(have as usize, true); + // If bitfield wasn't allocated yet, let's do it. Some clients send haves before bitfield. + if live.bitfield.is_empty() { + live.bitfield = + BF::from_vec(vec![0; self.state.lengths.piece_bitfield_bytes()]); + } + match live.bitfield.get_mut(have as usize) { + Some(mut v) => *v = true, + None => { + warn!("received have {} out of range", have); + return; + } + }; debug!("updated bitfield with have={}", have); }); }