Menu

cppcheck 2.10: false positive, internalAstError in constructor initialization list

2023-03-10
2023-03-11
  • Clint Chelak

    Clint Chelak - 2023-03-10

    I ran into autocode that has a curious use of braces in the constructor that causes an internalAstError. I recreated the issue below.

    // file CppcheckCtorDefinitionError.cpp
    
    #include <cstdint>
    typedef std::uint8_t UInt8;
    typedef std::uint32_t UInt32;
    
    struct ThingWithPath
    {
      ThingWithPath();
    
      UInt8* pFilePath;
      UInt32 filePathElements;
    };
    
    ThingWithPath::ThingWithPath()
      : pFilePath{new UInt8[512]{}}, // the error occurs here with the empty "{}"
        filePathElements{0}
    {}
    
    int main()
    {
      ThingWithPath t;
      return 0;
    }
    

    Command run: cppcheck --enable=all --inline-suppr --xml CppcheckCtorDefinitionError.cpp

    XML prints the following.

    <?xml version="1.0" encoding="UTF-8"?>
    <results version="2">
        <cppcheck version="2.10"/>
        <errors>
            <error id="internalAstError" severity="error" msg="Syntax error. Unexpected tokens in initializer." verbose="Syntax error. Unexpected tokens in initializer." file0="CppcheckCtorDefinitionError.cpp">
                <location file="CppcheckCtorDefinitionError.cpp" line="14" column="29"/>
            </error>
            <error id="missingIncludeSystem" severity="information" msg="Cppcheck cannot find all the include files (use --check-config for details)" verbose="Cppcheck cannot find all the include files. Cppcheck can check the code without the include files found. But the results will probably be more accurate if all the include files are found. Please check your project&apos;s include directories and add all of them as include directories for Cppcheck. To see what files Cppcheck cannot find use --check-config."/>
        </errors>
    </results>
    

    The second error might be hinting at a configuration issue on my machine. Running on WSL2.0 (particularly, 5.15.90.1-microsoft-standard-WSL2). Used Ubuntu 10.3.0-1ubuntu1~20.04 to build cppcheck. I confirmed the error did not occur on cppcheck 2.9, and the older version also caught the unused variable in main.

    I confirmed that the code compiled for C++11, 14, 17.

     
  • CHR

    CHR - 2023-03-11

    Thanks for reporting, I have added your example here: https://trac.cppcheck.net/ticket/11378#comment:16

     

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.