Menu

False positive: danglingLifetime, double pointer

2021-08-11
2021-08-11
  • Geongu.Park

    Geongu.Park - 2021-08-11

    I write this code and run the cppcheck.

    #include <stdio.h>
    #include <stdlib.h>
    
    static int sink;
    
    void foo()
    {
        int **pp = NULL;
        int *p = NULL;
        int a = 20;
        int ret;
        p = &a;
        pp = &p;
        ret = **pp; /*Tool should not detect this line as error*/ /*No ERROR:Uninitialized pointer*/
        sink = ret;
    }
    
    int main()
    {
        foo();
        return 0;
    }
    

    And I get the danglingLifetime error. I think this case is not error. What do you think?

    Checking E:\test2\test2\test.cpp ...
    E:\test2\test2\test.cpp:15:2: error: Non-local variable 'sink' will use object that points to local variable 'a'. [danglingLifetime]
    sink = ret;
    ^
    E:\test2\test2\test.cpp:12:6: note: Address of variable taken here.
    p = &a;
    ^
    E:\test2\test2\test.cpp:10:6: note: Variable created here.
    int a = 20;
    ^
    E:\test2\test2\test.cpp:15:2: note: Non-local variable 'sink' will use object that points to local variable 'a'.
    sink = ret;
    ^

     

    Last edit: Geongu.Park 2021-08-11
  • CHR

    CHR - 2021-08-11

    Thanks for the report, I have created a ticket here: https://trac.cppcheck.net/ticket/10406

     

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.