#3 Memory leak in error scenario.

closed-fixed
nobody
5
2009-10-29
2009-10-24
Anonymous
No

During code walkthrough, I noticed in print_object function if the print_value function fails the function is returning without freeing the str pointer from the print_str_ptr(child->string) function.

Thanks
Suriya.

static char *print_object(cJSON *item,int depth)
{
char *out,*ptr,*ret,*str;int len=7,i;
cJSON *child=item->child;

depth++;out=malloc(len+depth);*out='{';
ptr=out+1;*ptr++='\n';*ptr=0;
while (child)
{
str=print_string_ptr(child->string);
if (!str) {free(out);return 0;}
ret=print_value(child,depth);
if (!ret) {free(out);return 0;} // Check for failure!
len+=strlen(ret)+strlen(str)+4+depth;

Discussion

  • Dave Gamble
    Dave Gamble
    2009-10-29

    • status: open --> closed-fixed
     
  • Dave Gamble
    Dave Gamble
    2009-10-29

    Thanks! Fixed in svn rev 9!