MISRA Discussion Forums
6-5-2 and 6-5-4 on while loop checking std::vector::empty() - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=135)
+---- Thread: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() (/showthread.php?tid=1585)



6-5-2 and 6-5-4 on while loop checking std::vector::empty() - cgpzs - 26-10-2021

Hi,

We have a static analyzer that is reporting warnings related to 6-5-2 and 6-5-4 in the following code:

Code:
    std::vector<std::int32_t> v{};
    // Fill vector
    // ...
  
    // Consume vector
    while (!v.empty())  // <<< 6-5-2, 6-5-4 violated here?
    {
        std::int32_t const value{v.back()};
        v.pop_back();
        std::cout << value << "\n";
    }

The warning is that the "loop counter v" is not compared against a relational operator (6-5-2) nor is it modified via ++ et al. (6-5-4).

The static analysis tool vendor claims this is not a false positive and indeed it's required by the MISRA rules.

Could you shed some light as to whether the code above is violating 6-5-2 and 6-5-4? Is "v" really a loop counter as per the MISRA definition? One could argue that the loop counter is the private member std::vector::__size, but is that really the scope of this rule?

This code pattern is very typical when implementing e.g. graph search, having a list of Nodes and inspecting them one by one until a condition is reached or the list of nodes is empty. The list of nodes can typically grow while inspecting each node, so iterator-based searches are not a good implementation alternative.


RE: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() - misra cpp - 12-11-2021

See our earlier reply to 'Rule 6-6-3 in while-loops'

... there have been a number of previous posts about whether rules 6-5-1 to 6-5-6 apply to while loops as well as for loops. The reply to “Rules 6-5-1 to 6-5-6” by dg1980 April 28 2016 (in section 6.5 of the bulletin board) was:

6-5-1 only applies to for loops, arguably 6-5-2 to 6-5-6 may apply to while loops as well.

These rules are under consideration in light of this and MISRA C:2012's approach


RE: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() - cgpzs - 12-11-2021

Hi,

I have indeed read through the whole forum about this and I'm well aware that the rules apply to "while" loops as well.

But my question is: is the above code using a "loop counter" at all? If the answer is "no", then none of the rules related to loop counters apply. Would you agree?
Thanks!


RE: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() - misra cpp - 18-11-2021

In this case, you are right, there is no loop counter, so the rules don't apply.

The definitions in the introductory section are expressed in terms of the 'for loop' syntax, and show the equivalence of a 'for loop' and 'while loop'. This shows the while loop as 'while (condition)'. The definition of loop-counter requires it to be a loop-control-variable that is "an operand to a relational operator in condition". As there are no relational operators in the condition in the example code, there is no loop-counter


RE: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() - cgpzs - 18-11-2021

Thanks for the clarification!


RE: 6-5-2 and 6-5-4 on while loop checking std::vector::empty() - misra cpp - 11-01-2022

This thread is now closed


Anyone having a related question, please start a new thread.