Skip to content

Commit

Permalink
fix(cpps): Out-of-range access to vector
Browse files Browse the repository at this point in the history
std::uniform_int_distribution returns a number
from the closed interval of the lower and upper bound.
This sometimes lead to out-of-range access to the
possible_candidates vector.
Further, the code was simplified by always using the
std::uniform_int_distribution, even with only a single
element.
  • Loading branch information
ltoenning committed Sep 27, 2023
1 parent 49c3c9a commit ef8206b
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,12 @@ RoundRobinInitiator::chooseParticipantForTask(const material_flow::Task &task) {
if (possible_candidates.empty()) {
throw std::runtime_error("No AMR matches the task requirements");
}
int random_index = 0;
if (possible_candidates.size() > 1) {
// choose AMR randomly
std::uniform_int_distribution<uint64_t> dist(0, possible_candidates.size());
random_index = dist(daisi::global_random_engine);
}

// choose AMR randomly
std::uniform_int_distribution<uint64_t> dist(0, possible_candidates.size() - 1);
const uint64_t random_index = dist(daisi::global_random_engine);
auto chosen_participant =
std::make_shared<ParticipantInfoRoundRobin>(possible_candidates[random_index]);
std::make_shared<ParticipantInfoRoundRobin>(possible_candidates.at(random_index));

return chosen_participant;
}
Expand Down

0 comments on commit ef8206b

Please sign in to comment.