Menu

Missing result: for loop variable mismatch -> infinite loop

2020-05-27
2020-05-27
  • Glenn Storm

    Glenn Storm - 2020-05-27

    This one I only happen to catch a) because it is a method I hadn't utilitzed in my project yet and. b) because there was another style or performance results that pointed within the scope of this for loop. But clearly this is a good candidate for a proper 'infinite loop' result of some kind.

    for (int p=0; i<m_someVector.size(); i++) {
        // doing things with the vector and the variable 'p'
        //   as in, m_someVector[p] blah, blah, blah
        // but _oops_ check out the typo in the for loop !
        // that would have led to an infinite loop
    }
    

    I caught this 'manually' while the result indicated a line within the for loop scope.

    'Hope that helps.

     
  • Daniel Marjamäki

    I do not think there is anything to warn about in the shown code, sometimes there are different variables in a for loop statement by intention.
    but well if we looked at the code in the loop also we might see that the looping is redundant or something. Redundant stuff is something I feel we can write a stylistic message about.

     
  • Daniel Marjamäki

    Thanks for the suggestion! Feel free to suggest more stuff if you see some bugs..

     
  • Glenn Storm

    Glenn Storm - 2020-05-27

    I hear you. If cppcheck isn't tracking how "p" would not change for however many times "i" needs to do something, I can see how that might be valid. (I thought if "p" is used as the vector index, and p is never changed in the scope of the loop, perhaps this would be a good warning)

    Thanks for looking at it!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.