clicon / clixon

YANG-based toolchain including NETCONF and RESTCONF interfaces and an interactive CLI
http://www.clicon.org/
Other
215 stars 72 forks source link

A problem was found in Reviewing the code #391

Closed lihailong516 closed 1 year ago

lihailong516 commented 1 year ago

hello In function clicon_file_dirent, there may be a memory leak occurs. image

olofhagsand commented 1 year ago

I dont think so. The variable "ent" is defined as an "out" parameter on line 166:

* @param[out] ent     Entries pointer, will be filled in with dir entries. Free

("Free" should really be "Free after use"). This means that the pointer value of the local "new" variable is transferred to "ent" on line 251, and in order for it to not be freed later on line 256, its value is set to NULL. In other circumstances, such as an error condition (ie "goto done"), "ret" will be freed. This means that it is up to the caller of the function to free the "ent" parameter after use. If this is not done, there will be a memory leak. The example code in the function comment describes this:

    if ((ndp = clicon_file_dirent(dir, &dp, "(.so)$", S_IFREG)) < 0)
        return -1;
    for (i = 0; i < ndp; i++) 
        do something with dp[i].d_name;
    free(dp);  <----
lihailong516 commented 1 year ago

Thanks for the answer, the analysis lacks consideration