Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
6-5-2 and 6-5-4 on while loop checking std::vector::empty()
#1
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.
Reply
#2
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
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#3
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!
Reply
#4
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
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#5
Thanks for the clarification!
Reply
#6
This thread is now closed


Anyone having a related question, please start a new thread.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)