Menu

Wrong report for index out of bounds

2023-07-25
2023-07-25
  • huanghaifeng

    huanghaifeng - 2023-07-25

    Below is a simple code:

    #define M 2
    
    struct Key {
      int sts;
    };
    typedef struct Key keytype;
    
    typedef struct page {
      keytype key[2 * M];
    } *pageptr;
    
    pageptr NewPage();
    
    void BkSplit(pageptr p, int k) {
      int j, m;
      pageptr q;
    
      if (k <= M) {
        m = M;
      } else {
        m = M + 1;
      }
    
      q = Newpage();
       for (j = m + 1; j <= 2 * M; j++) {
         q->key[j-m-1] = p->key[j-1];
       }
    }
    

    When I run cppcheck, It outputs below error:

    [C:/Users/xxxx/Desktop/cppcheck.c:27] (error) Array 'q->key[4]' accessed at index -1, which is out of bounds. [negativeIndex]

    I can sure there is no 'index out of bounds.

    How to solve this problem?

     
  • CHR

    CHR - 2023-07-25

    Thanks for reporting this false positive. Ticket is here: https://trac.cppcheck.net/ticket/11844

     

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.