Menu

constParameter False-Positive when using a PIMPLd class and references

2021-09-09
2021-09-11
  • Steve Albright

    Steve Albright - 2021-09-09

    I'm using 2.5

    This is a false-positive because buffer actually gets written too

    constParameter - Parameter 'buffer' can be declared with const

    namespace constParameterFalsePositive
    {
       class CustomBuffer
       {
       public:
          CustomBuffer()
             : Data()
             , Position(0)
          {
          }
    
          CustomBuffer& operator<<(char value)
          {
             if(Size > Position)
             {
                Data[Position++] = value;
             }
    
             return *this;
          }
    
       private:
          static const int Size = 256;
          unsigned char Data[Size];
          int Position;
       };
    
       class SerializeHelper
       {
       public:
          // This causes cppcheck to loose context and not realize the buffer is changed calling
          // the function below. This simulates having a pimpled class for ABI compatibility reasons.
          // In the case where this was discovered we are using the private implementation and not 'this'.
          SerializeHelper* getPrivatePtr() { return this; }
    
          CustomBuffer& operator<<(CustomBuffer& buffer)
          {
             buffer << '2';
             return buffer;
          }
       };
    
       CustomBuffer& operator<<(CustomBuffer& buffer, SerializeHelper& rhs)
       {
          // the problem is caused by using getPrivatePtr and loss of context
          return rhs.getPrivatePtr()->operator<<(buffer);
       }
    }
    
     

    Last edit: Steve Albright 2021-09-09
  • Daniel Marjamäki

    Sorry for slow response. I can reproduce and will work on fixing it..

     

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.