Menu

objectIndex False-Positive when reading bytes out of larger data types

2021-09-09
2021-09-27
  • Steve Albright

    Steve Albright - 2021-09-09

    I'm using 2.5

    error: objectIndex - The address of local variable 'attributes' is accessed at non-zero index.

    #include <vector>
    
    namespace objectIndexFalsePositive
    {
       void ExampleOfObjectIndexFalsePositive(
             unsigned short mask1,
             unsigned short mask2,
             unsigned short mask3)
       {
          std::vector<unsigned char> messageBuffer;
          unsigned short attributes = 0;
    
          attributes |= mask1;
          attributes |= mask2;
          attributes |= mask3;
    
          unsigned char* charAttributes = reinterpret_cast<unsigned char*>(&attributes);
    
          messageBuffer.push_back(charAttributes[1]); // this should be the second byte of the unsigned short and is valid
          messageBuffer.push_back(charAttributes[0]);
       }
    }
    
     
  • Steve Albright

    Steve Albright - 2021-09-23

    Another similar example, not sure if it warrants a different issue

    uint ToInt(const QByteArray& data)
    {
       uint value = 0;
       char* valueChar = reinterpret_cast<char*>(&value);
       const int size = std::min(static_cast<int>(sizeof(uint)), data.size());
    
       for(int index = 0; index < size; ++index)
       {
          valueChar[index] = data[index];
       }
    
       return value;
    }
    
     
  • CHR

    CHR - 2021-09-27

    Here is a related ticket: https://trac.cppcheck.net/ticket/10154

     

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.