Menu

Crash when parsing a class with a static template method

2021-12-15
2021-12-15
  • Catalin Popescu

    Catalin Popescu - 2021-12-15

    I use cppcheck version 2.6.

    Below is a class that has a static template method (create()):

    // CAbc.h
    #pragma once
    class CAbc
    {
    
    public:
    
        template<class D>
        static CAbc *create()
        {
            CAbc *pCtrl = new CAbc();
            return pCtrl;
        }
    
        ~CAbc();
    
    private:
        CAbc();
    };
    
    CAbc::CAbc()
    {
    }
    
    CAbc::~CAbc()
    {
    }
    

    The 'create()' method is used in another source file:

    // Caller.cpp
    #include "CAbc.h"
    
    void test()
    {
        CAbc * p = CAbc::create<int>();
    }
    

    I use the following command:

    cppcheck -URM -URM_OLD -URM_OLD_DELEGATES --enable=all --inconclusive --includes-file=CppCheck-IncludePaths.cfg  --force --xml --xml-version=2 --platform=win64  .. 2> result.xml
    

    Cppcheck exits before finishing the 'result.xml', leaving it incomplete.
    If I run cppcheck in debug mode, a debug assertion fails in TemplateSimplifier::expandTemplate():

    void TemplateSimplifier::expandTemplate(
        const TokenAndName &templateDeclaration,
        const TokenAndName &templateInstantiation,
        const std::vector<const Token *> &typeParametersInDeclaration,
        const std::string &newName,
        bool copy)
    {
    //...........  
                    // link() newly tokens manually
                else if (copy) {
                    if (tok3->str() == "{") {
                        brackets.push(mTokenList.back());
                    } else if (tok3->str() == "(") {
                        brackets.push(mTokenList.back());
                    } else if (tok3->str() == "[") {
                        brackets.push(mTokenList.back());
                    } else if (tok3->str() == "}") {
                        assert(brackets.empty() == false); // HERE, THE ASSERTION FAILS
                        assert(brackets.top()->str() == "{");
                        Token::createMutualLinks(brackets.top(), mTokenList.back());
                        //...............
                        }
    

    I found these temporary solutions to avoid the crash:
    1. add a semicolon after the body of the 'create()' method
    2. move the 'create()' method implementation outside of the class

     
  • CHR

    CHR - 2021-12-15

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

     

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.