Menu

Could cppcheck returnDanglingLifetime with nested function member in class?

mark zhang
2024-08-07
2024-08-08
  • mark zhang

    mark zhang - 2024-08-07

    Hello experts,
    Recently I'm trying to use the cppcheck to do some analysis of code, and got following question:
    Sample of code:

    #include <string>
    using namespace std;
    class A
    {
    public:
      static A & getInstance()
      {
        return theInstance;
      }
      string getValue();
    
    private:
      static A theInstance;
    };
    
    string getValue();
    
    const char *get()
    {
      const char* theAns;
      //theAns=getValue().c_str();/// When use the plain function call could get the expect warning result
      theAns=A::getInstance().getValue().c_str(); /// But not for this one.
      return theAns;
    }
    
    int main()
    {
    cont char * a = get();
    printf("a=%s\n", a);
    return 1;
    }
    

    Could you help check if cppcheck support such scenario?

    Thanks,
    Mark

     

    Last edit: mark zhang 2024-08-07
  • mark zhang

    mark zhang - 2024-08-08

    Got answer, the latest version 2.14 could warn on this case.
    But I used version 2.4.

    Thanks,
    Mark

     

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.