Skip to content

Commit

Permalink
Use std::next to avoid debug asserts on vector access
Browse files Browse the repository at this point in the history
When building in debug mode (at least on windows) vector access
is checked such that we can't use operator[] to point to what is
normally std::vector::end() since it triggers an assert.

Use std::next to advance instead.
  • Loading branch information
Zitrax committed Aug 20, 2023
1 parent 18f088a commit 23ca796
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ class HandshakeMsg {
console->debug("Wait for more handshake data...");
return make_optional<HandshakeMsg>(reserved, info_hash, peer_id, 0);
}
Bitfield bf(bytes(&msg[73], &msg[73 + len - 1]));
Bitfield bf(
bytes(std::next(msg.begin(), 73), std::next(msg.begin(), end)));
console->debug("Handshake: {}", bf);
// Consume the parsed part, the caller have to deal with the rest
return make_optional<HandshakeMsg>(reserved, info_hash, peer_id,
73 + len - 1, bf);
return make_optional<HandshakeMsg>(reserved, info_hash, peer_id, end, bf);
}

return make_optional<HandshakeMsg>(reserved, info_hash, peer_id,
Expand Down Expand Up @@ -277,8 +277,8 @@ size_t Message::parse(PeerConnection& connection) {
return 9;
}
case peer_wire_id::BITFIELD: {
auto start = m_msg.begin() + 5;
auto end = start + len - 1;
auto start = std::next(m_msg.begin(), 5);
auto end = std::next(start, len - 1);
auto bf = Bitfield(bytes(start, end));
peer.set_remote_pieces(bf);
m_logger->debug("{}: {}", peer.str(), bf);
Expand Down

0 comments on commit 23ca796

Please sign in to comment.