Menu

Is this a false possitive?

Fernando R
2017-02-21
2017-02-21
  • Fernando R

    Fernando R - 2017-02-21

    Before creating a new issue in the "trac" system, I would like to make sure
    this is not the expected behaviour.

    The next piece of C code generates a memoryleak error:

    * START *****
    T2 x;

    void f(void)
    {

    x.a = (int *) malloc(sizeof(int) * 10);

    if (x.a != NULL)
    {
    free(x.a);
    x.a = NULL;
    }

    }
    * END *****

    $ cppcheck test.c
    Checking test.c...
    [test.c:11]: (error) Memory leak: a

    Note that the "T2" type is unknown.

    If, instead of an "unknown" type, we use a well defined one, then the error
    dissapears:

    * START ****
    typedef struct
    {
    int
    a;
    } T;

    T x;

    void f(void)
    {

    x.a = (int *) malloc(sizeof(int) * 10);

    if (x.a != NULL)
    {
    free(x.a);
    x.a = NULL;
    }

    }
    * END *****

    I known I should use #include's in the first example and tell cppcheck
    where to look for the header files where the type is defined. However, when
    the type is not defined, is it the expected behaviour to generate a
    memoryleak error? What's the logic behind this decission?

    Thanks!

     
  • Daniel Marjamäki

    That is a false positive. Please report it in our issue tracker.

     

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.