Skip to content

Commit aadc0c9

Browse files
committed
Fix the possible infinite loop warning on GetChoices() in result.cpp
1 parent 988da21 commit aadc0c9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/gridcoin/voting/result.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,21 @@ class LegacyVoteCounterContext
633633
const LegacyChoiceMap& GetChoices()
634634
{
635635
if (m_legacy_choices_cache.empty()) {
636-
for (uint8_t i = 0; i < m_poll.Choices().size(); ++i) {
636+
uint8_t m_poll_choices_size = 0;
637+
638+
// This silences the code scan warning of a possible infinite loop due to the overflow of the uint8_t i counter
639+
// against the comparison to m_poll.Choices.size(). Note it is the logged equivalent to std::min<uint8_t>. Since
640+
// this is legacy polls only and those are no longer issued, and this condition does not actually exist in the
641+
// chain, this is purely for formality's sake.
642+
if (m_poll.Choices().size() > (size_t) std::numeric_limits<uint8_t>::max()) {
643+
LogPrintf("WARN: %s: Number of legacy poll choices exceeds bins available in m_legacy_choices_cache map. "
644+
"Limiting to %u.", __func__, std::numeric_limits<uint8_t>::max());
645+
m_poll_choices_size = std::numeric_limits<uint8_t>::max();
646+
} else {
647+
m_poll_choices_size = m_poll.Choices().size();
648+
}
649+
650+
for (uint8_t i = 0; i < m_poll_choices_size; ++i) {
637651
std::string label = m_poll.Choices().At(i)->m_label;
638652
label = ToLower(label);
639653

0 commit comments

Comments
 (0)