Menu

False positives with bitfields and a modified platform integer size

2025-05-14
2025-05-15
  • Chris Engel

    Chris Engel - 2025-05-14

    We are running into an issue where it appears cppcheck is not calculating the size of an object correctly and is then calling out passedByValue

    Here is the sample object with a bunch of different variations that all lead to an 8B value . Some work and others don't as denoted by the comments.

    class   testObject
    {
      public:
        enum Base
        {
          Val1 = 0x0000
        };
    
        #if 0  // This fails
        Base                 base:32;
        Base                 ext:16;
        uint16_t             value;
        #elif 0 // This works
        Base                 base:32;
        uint32_t             value;
        #elif 0 // This works
        uint32_t             base;
        Base                 ext:16;
        uint16_t             value;
        #elif 0 // This fails
        Base                 base:32;
        Base                 ext:16;
        uint16_t             value;
        #elif 0 // This fails
        Base                 base1:16;
        Base                 base2:16;
        Base                 ext:16;
        uint16_t             value;
        #elif 0 // This works
        Base                 base1:32;
        Base                 base2:32;
        #elif 0 // This works
        Base                 base1:32;
        uint16_t             value;
        uint16_t             value2;
        #elif 1 // This fails
        Base                 base1:16;
        Base                 base2:16;
        uint16_t             value;
        uint16_t             value2;
    
        #endif
    };
    

    I'm running cppcheck 2.17.1 as follows:

    $ cppcheck -q --enable=performance --language=c++ --template=gcc --platform=./platform.xml test.C
    test.C:51:23: warning: Function parameter 'tParm' should be passed by const reference. [passedByValue]
    bool func1(testObject tParm) {
                          ^
    

    And our platform file has only one change from the unit64-unsigned.xml platform file provided by cppcheck

    $ diff platform.xml unix64-unsigned.xml 
    8c8
    <     <int>8</int>
    ---
    >     <int>4</int>
    
     
  • CHR

    CHR - 2025-05-15

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

     

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.