Menu

#64 found a memory leak in file exp_clib.c

closed-fixed
None
5
2014-10-09
2009-06-29
orbitcowboy
No

Hello all,

i have checked the sources of expect with a static code analysis tool (cppcheck). It found a memory leak in file exp_clib.c at line 403.
Cppcheck prints the following output:

expect-5.43$ cppcheck -a -v -f -j2 -q .
[./exp_clib.c:403]: (error) Memory leak: r

Take a look at the file exp_clib.c:
....
r = (regexp *)ckalloc(sizeof(regexp) + (unsigned)rcstate->regsize);
if (r == NULL)
FAIL("out of space");

/* Second pass: emit code. */
rcstate->regparse = exp;
rcstate->regnpar = 1;
rcstate->regcode = r->program;
regc(MAGIC, rcstate);
if (reg(0, &flags, rcstate) == NULL)
403 return(NULL);
...

As you can see, the function returns without freeing the allocated memory.
A possible way out is following code snipped:

r = (regexp *)ckalloc(sizeof(regexp) + (unsigned)rcstate->regsize);
if (r == NULL)
FAIL("out of space");

/* Second pass: emit code. */
rcstate->regparse = exp;
rcstate->regnpar = 1;
rcstate->regcode = r->program;
regc(MAGIC, rcstate);
if (reg(0, &flags, rcstate) == NULL)
{
free(r);
return(NULL);
}

.....

Best regards

Ettl Martin

Discussion

  • Andreas Kupries

    Andreas Kupries - 2009-07-14

    Confirming that this looks like a leak. Accepted the change, except that we are using 'ckfree', not 'free', like the allocation was done by 'ckalloc', not 'malloc'.

     
  • Andreas Kupries

    Andreas Kupries - 2009-07-14
    • assigned_to: nobody --> andreas_kupries
    • status: open --> closed-fixed
     

Log in to post a comment.