Menu

double initilisation on a for loop leads to false positive

2023-06-20
2023-06-20
  • Yannick HOUDOT

    Yannick HOUDOT - 2023-06-20

    Hi, did you already saw this one ?

    static const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    int main (int n, char *s)
      {
      int i;
      unsigned long m;
      char d[4];
    
          m = 0;
        for (i = 0; i < n; i++)
          m = (m << 8) | *s++;
        for (; i < 3; i++)
          m <<= 8;
        for (i++; i > 0; m >>= 6)
          d[--i] = alpha[m & 0x3F];  // cppcheck False +
      }
    

    $ /opt/cppcheck-2.10/cppcheck --std=c99 --enable=all t6.c
    Checking t6.c ...
    t6.c:15:8: error: Array 'd[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]
    d[--i] = alpha[m & 0x3F]; // cppcheck False +
    ^
    t6.c:12:14: note: Assuming that condition 'i<3' is not redundant
    for (; i < 3; i++)
    ^
    *t6.c:14:10: note: i is incremented', new value is 4
    for (i++; i > 0; m >>= 6)
    ^
    t6.c:14:10: note: i is incremented', new value is 5
    for (i++; i > 0; m >>= 6)
    ^

    t6.c:15:8: note: Array index out of bounds
    d[--i] = alpha[m & 0x3F]; // cppcheck False +
    ^

    $

     
  • CHR

    CHR - 2023-06-20

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

     

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.