Menu

constParameterCallback false-positive with memcpy

2021-09-14
2021-09-14
  • Steve Albright

    Steve Albright - 2021-09-14

    constParameter - Parameter 'DataToEncrypt' can be declared with const

    It isn't recognizing that memcpy can affect the data.

    This might be a simpler way to reproduce this one https://sourceforge.net/p/cppcheck/discussion/general/thread/dda510831e/

    namespace constParameterCallbackFalsePostive
    {
       struct DataToEncrypt
       {
          // cppcheck-suppress unusedStructMember - submitted separately
          static const int ENCRYPTED_DATA_BUF_SIZE = 128;
    
          unsigned char EncryptedDataBuf[ENCRYPTED_DATA_BUF_SIZE];
       };
    
       void Encrypt(DataToEncrypt& DataToEncrypt)
       {
          unsigned char data[] = "asfasd";
    
          memcpy(DataToEncrypt.EncryptedDataBuf, &data, sizeof(data));
       }
    }
    
     
  • CHR

    CHR - 2021-09-14

    Oddly, there is no FP if the source parameter of memcpy is not const void*:

       //void mc(void* dst, const void* src, int size); // FP
       void mc(void* dst, const char* src, int size); // no FP
    
       struct DataToEncrypt
       {
          char EncryptedDataBuf[128];
       };
    
       void Encrypt(DataToEncrypt& DataToEncrypt)
       {
          char data[] = "asfasd";
    
          mc(DataToEncrypt.EncryptedDataBuf, &data, sizeof(data));
       }
    
     
  • CHR

    CHR - 2021-09-14
     

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.