From af4a953cd1556f9d6fc1dd799b16a9d2d43a9a7e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 7 Nov 2021 18:25:45 -0600 Subject: [PATCH] fixup! refactor: remove tr_piece struct (#2059) (#2115) yet another fixup of the tr_piece struct removal --- libtransmission/bitfield.cc | 11 +++-------- libtransmission/bitfield.h | 2 +- libtransmission/peer-msgs.cc | 2 +- libtransmission/resume.cc | 4 ++-- tests/libtransmission/bitfield-test.cc | 2 +- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libtransmission/bitfield.cc b/libtransmission/bitfield.cc index 8e13341e7..9045e6f9c 100644 --- a/libtransmission/bitfield.cc +++ b/libtransmission/bitfield.cc @@ -292,18 +292,13 @@ void tr_bitfield::setHasAll() TR_ASSERT(assertValid()); } -void tr_bitfield::setRaw(uint8_t const* raw, size_t byte_count, bool bounded) +void tr_bitfield::setRaw(uint8_t const* raw, size_t byte_count) { - if (bounded) - { - byte_count = std::min(byte_count, getBytesNeeded(bit_count_)); - } - flags_ = std::vector(raw, raw + byte_count); - if (bounded) + // ensure any excess bits at the end of the array are set to '0'. + if (byte_count == getBytesNeeded(bit_count_)) { - /* ensure the excess bits are set to '0' */ int const excess_bit_count = byte_count * 8 - bit_count_; TR_ASSERT(excess_bit_count >= 0); diff --git a/libtransmission/bitfield.h b/libtransmission/bitfield.h index 258e0659f..6d0a9cb76 100644 --- a/libtransmission/bitfield.h +++ b/libtransmission/bitfield.h @@ -63,7 +63,7 @@ public: // "raw" here is in BEP0003 format: "The first byte of the bitfield // corresponds to indices 0 - 7 from high bit to low bit, respectively. // The next one 8-15, etc. Spare bits at the end are set to zero. - void setRaw(uint8_t const* bits, size_t byte_count, bool bounded); + void setRaw(uint8_t const* bits, size_t byte_count); std::vector raw() const; [[nodiscard]] constexpr bool hasAll() const diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index 1fafaf315..0bbda2364 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -1715,7 +1715,7 @@ static ReadState readBtMessage(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, si uint8_t* tmp = tr_new(uint8_t, msglen); dbgmsg(msgs, "got a bitfield"); tr_peerIoReadBytes(msgs->io, inbuf, tmp, msglen); - msgs->have.setRaw(tmp, msglen, tr_torrentHasMetadata(msgs->torrent)); + msgs->have.setRaw(tmp, msglen); msgs->publishClientGotBitfield(&msgs->have); updatePeerProgress(msgs); tr_free(tmp); diff --git a/libtransmission/resume.cc b/libtransmission/resume.cc index a91b197d5..790feff6f 100644 --- a/libtransmission/resume.cc +++ b/libtransmission/resume.cc @@ -485,7 +485,7 @@ static void rawToBitfield(tr_bitfield& bitfield, uint8_t const* raw, size_t rawl } else { - bitfield.setRaw(raw, rawlen, true); + bitfield.setRaw(raw, rawlen); } } @@ -648,7 +648,7 @@ static uint64_t loadProgress(tr_variant* dict, tr_torrent* tor) } else if (tr_variantDictFindRaw(prog, TR_KEY_bitfield, &raw, &rawlen)) { - blocks.setRaw(raw, rawlen, true); + blocks.setRaw(raw, rawlen); } else { diff --git a/tests/libtransmission/bitfield-test.cc b/tests/libtransmission/bitfield-test.cc index 65eb09b77..be071b54e 100644 --- a/tests/libtransmission/bitfield-test.cc +++ b/tests/libtransmission/bitfield-test.cc @@ -99,7 +99,7 @@ TEST(Bitfield, setRaw) auto const raw = std::vector(100, TestByte); auto bf = tr_bitfield(std::size(raw) * 8); - bf.setRaw(std::data(raw), std::size(raw), true); + bf.setRaw(std::data(raw), std::size(raw)); EXPECT_EQ(TestByteTrueBits * std::size(raw), bf.count()); // The first byte of the bitfield corresponds to indices 0 - 7