Menu

Out of bounds check failure

2023-12-22
2023-12-22
  • Simon Morris

    Simon Morris - 2023-12-22

    Hi,

    void test_function()
    {
    int myArray[10];

    for (int i = 0; i < 20; i++)
    {
        myArray[i] = i * 5; 
    }
    
    ...
    

    }

    Please can I ask why Cppcheck doesn't detect the "obvious" out of bounds error in the code?

     
  • CHR

    CHR - 2023-12-22
    test.cpp:7:12: error: Array 'myArray[10]' accessed at index 19, which is out of bounds. [arrayIndexOutOfBounds]
        myArray[i] = i * 5;
               ^
    test.cpp:5:19: note: Assuming that condition 'i<20' is not redundant
    for (int i = 0; i < 20; i++)
                      ^
    test.cpp:7:12: note: Array index out of bounds
        myArray[i] = i * 5;
               ^
    

    Works for me with head.

     

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.