From 2bb0363ae1e68d96d4afff99186b5a772d185cc7 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Sat, 11 Nov 2023 18:54:58 +0100 Subject: [PATCH] iox-#2047 Fix 'array-bounds' warning in chunk distributor --- .../building_blocks/chunk_distributor.hpp | 2 + .../building_blocks/chunk_distributor.inl | 42 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.hpp b/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.hpp index 9e7e35f3b82..f193c6205b2 100644 --- a/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.hpp +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.hpp @@ -24,6 +24,8 @@ #include "iox/detail/unique_id.hpp" #include "iox/not_null.hpp" +#include +#include #include namespace iox diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.inl b/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.inl index 2db654d8dcc..623ec524085 100644 --- a/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.inl +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/building_blocks/chunk_distributor.inl @@ -140,7 +140,8 @@ template inline uint64_t ChunkDistributor::deliverToAllStoredQueues(mepoo::SharedChunk chunk) noexcept { uint64_t numberOfQueuesTheChunkWasDeliveredTo{0U}; - typename ChunkDistributorDataType::QueueContainer_t remainingQueues; + using QueueContainer = decltype(getMembers()->m_queues); + QueueContainer fullQueuesAwaitingDelivery; { typename MemberType_t::LockGuard_t lock(*getMembers()); @@ -158,7 +159,7 @@ inline uint64_t ChunkDistributor::deliverToAllStoredQu { if (isBlockingQueue) { - remainingQueues.emplace_back(queue); + fullQueuesAwaitingDelivery.emplace_back(queue); } else { @@ -171,43 +172,40 @@ inline uint64_t ChunkDistributor::deliverToAllStoredQu // busy waiting until every queue is served iox::detail::adaptive_wait adaptiveWait; - while (!remainingQueues.empty()) + while (!fullQueuesAwaitingDelivery.empty()) { adaptiveWait.wait(); { - // create intersection of current queues and remainingQueues + // create intersection of current queues and fullQueuesAwaitingDelivery // reason: it is possible that since the last iteration some subscriber have already unsubscribed // and without this intersection we would deliver to dead queues typename MemberType_t::LockGuard_t lock(*getMembers()); - typename ChunkDistributorDataType::QueueContainer_t queueIntersection(remainingQueues.size()); - auto greaterThan = [](RelativePointer& a, RelativePointer& b) -> bool { + QueueContainer remainingQueues; + using QueueContainerValue = typename QueueContainer::value_type; + auto greaterThan = [](QueueContainerValue& a, QueueContainerValue& b) -> bool { return reinterpret_cast(a.get()) > reinterpret_cast(b.get()); }; std::sort(getMembers()->m_queues.begin(), getMembers()->m_queues.end(), greaterThan); - std::sort(remainingQueues.begin(), remainingQueues.end(), greaterThan); + std::sort(fullQueuesAwaitingDelivery.begin(), fullQueuesAwaitingDelivery.end(), greaterThan); - auto iter = std::set_intersection(getMembers()->m_queues.begin(), - getMembers()->m_queues.end(), - remainingQueues.begin(), - remainingQueues.end(), - queueIntersection.begin(), - greaterThan); - queueIntersection.resize(static_cast(iter - queueIntersection.begin())); - remainingQueues = queueIntersection; + std::set_intersection(getMembers()->m_queues.begin(), + getMembers()->m_queues.end(), + fullQueuesAwaitingDelivery.begin(), + fullQueuesAwaitingDelivery.end(), + std::back_inserter(remainingQueues), + greaterThan); + fullQueuesAwaitingDelivery.clear(); // deliver to remaining queues - for (uint64_t i = remainingQueues.size() - 1U; !remainingQueues.empty(); --i) + for (auto& queue : remainingQueues) { - if (pushToQueue(remainingQueues[i].get(), chunk)) + if (pushToQueue(queue.get(), chunk)) { - remainingQueues.erase(remainingQueues.begin() + i); ++numberOfQueuesTheChunkWasDeliveredTo; } - - // don't move this up since the for loop counts downwards and the algorithm would break - if (i == 0U) + else { - break; + fullQueuesAwaitingDelivery.push_back(queue); } } }