Menu

false positive arrayIndexOutOfBounds when using pre-increment in member function call

2024-10-16
2025-04-05
  • Yannick HOUDOT

    Yannick HOUDOT - 2024-10-16

    Hi Guys,

    It looks like increment is evaluated twice ... do you know of this issue ?
    change from post to pre-increment does not change anything.

    if I change to
    v[idx++].a =0;
    then it's ok, but as soon as I called a member function, it goes awry.

    void fct()
    {
        struct _st_t{
            int a; 
            void set(size_t i) { a=i; }
        } v[4];
    
        int idx = 0;
    
        v[idx++].set(0);
        v[idx++].set(0);
        v[idx++].set(0);
        v[idx++].set(0);
    }
    

    logs:

    $ cppcheck   test_incr_bound.cpp
    Checking test_incr_bound.cpp ...
    test_incr_bound.cpp:13:6: error: Array 'v[4]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds]
        v[idx++].set(0);
         ^
    test_incr_bound.cpp:9:15: note: Assignment 'idx=0', assigned value is 0
        int idx = 0;
                  ^
    test_incr_bound.cpp:11:7: note: idx is incremented', new value is 1
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:11:7: note: idx is incremented', new value is 2
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:12:7: note: idx is incremented', new value is 3
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:12:7: note: idx is incremented', new value is 4
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:13:7: note: idx is incremented', new value is 5
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:13:6: note: Array index out of bounds
        v[idx++].set(0);
         ^
    test_incr_bound.cpp:14:6: error: Array 'v[4]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]
        v[idx++].set(0);
         ^
    test_incr_bound.cpp:9:15: note: Assignment 'idx=0', assigned value is 0
        int idx = 0;
                  ^
    test_incr_bound.cpp:11:7: note: idx is incremented', new value is 1
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:11:7: note: idx is incremented', new value is 2
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:12:7: note: idx is incremented', new value is 3
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:12:7: note: idx is incremented', new value is 4
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:13:7: note: idx is incremented', new value is 5
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:13:7: note: idx is incremented', new value is 6
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:14:7: note: idx is incremented', new value is 7
        v[idx++].set(0);
          ^
    test_incr_bound.cpp:14:6: note: Array index out of bounds
        v[idx++].set(0);
         ^
    
    $ cppcheck --version
    Cppcheck 2.13.0
    
    $
    
     
  • CHR

    CHR - 2024-10-16

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

     
  • Yannick HOUDOT

    Yannick HOUDOT - 2025-04-04

    from the trac link, I thought the issue was corrected in 2.17.1 version... but It's still occurs.

    Did I misunderstand or does the defect fall into limbo ?

    (just asking !) Regards.

     
  • CHR

    CHR - 2025-04-05

    The ticket is still open, so unfortunately nothing has been fixed yet.

     

    Last edit: CHR 2025-04-05

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.