From 23ca7965c9db9be96c872a9b88a5563fc4b0cd79 Mon Sep 17 00:00:00 2001 From: Daniel Bengtsson Date: Sun, 20 Aug 2023 23:59:53 +0200 Subject: [PATCH] Use std::next to avoid debug asserts on vector access 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. --- src/messages.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/messages.cpp b/src/messages.cpp index d70c998..73890a5 100644 --- a/src/messages.cpp +++ b/src/messages.cpp @@ -176,11 +176,11 @@ class HandshakeMsg { console->debug("Wait for more handshake data..."); return make_optional(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(reserved, info_hash, peer_id, - 73 + len - 1, bf); + return make_optional(reserved, info_hash, peer_id, end, bf); } return make_optional(reserved, info_hash, peer_id, @@ -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);