Menu

containerOutOfBounds false positive

2024-05-14
2024-05-14
  • Steve Albright

    Steve Albright - 2024-05-14

    containerOutOfBounds - Out of bounds access in 'totals[index++]', if 'totals' size is 3 and 'index++' is 5

    class ExampleClass
    {
    public:
       void SetValue(int value) { Value = value; }
    
    private:
       int Value;
    };
    
    QVector<ExampleClass*> GetVector()
    {
       int index = 0;
       QVector<ExampleClass*> values;
    
       values.reserve(3);
       values.push_back(new ExampleClass());
       values[index++].SetValue(1000);
    
       values.push_back(new ExampleClass());
       values[index++].SetValue(2000);
    
       values.push_back(new ExampleClass());
       values[index++].SetValue(3000);
    
       return values;
    }
    

    It works fine if it is a vector of ints but gets confused once you try manipulating class values.

    it also works with integers instead of index++

     

    Last edit: Steve Albright 2024-05-14
  • CHR

    CHR - 2024-05-14

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/12731

     

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.