Commit f972ae13 authored by Rémi Duraffort's avatar Rémi Duraffort

vlm: fix memory leaks.

parent 51b83519
...@@ -227,6 +227,7 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim, ...@@ -227,6 +227,7 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
char* psz_childdelim = NULL; char* psz_childdelim = NULL;
char* psz_nametag = NULL; char* psz_nametag = NULL;
char* psz_response = strdup( "" ); char* psz_response = strdup( "" );
char *psz_tmp;
int i_success = 0; int i_success = 0;
int i; int i;
vlm_message_t *aw_child, **paw_child; vlm_message_t *aw_child, **paw_child;
...@@ -248,9 +249,11 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim, ...@@ -248,9 +249,11 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
/* Append name of child node, if not in a list */ /* Append name of child node, if not in a list */
if( !i_list ) if( !i_list )
{ {
i_success = asprintf( &psz_response, "%s\"%s\": ", i_success = asprintf( &psz_tmp, "%s\"%s\": ",
psz_response, aw_child->psz_name ); psz_response, aw_child->psz_name );
if( i_success == -1 ) break; if( i_success == -1 ) break;
free( psz_response );
psz_response = psz_tmp;
} }
/* If child node has children, */ /* If child node has children, */
...@@ -278,24 +281,28 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim, ...@@ -278,24 +281,28 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
strcmp( aw_child->psz_name, "inputs" ) == 0 || strcmp( aw_child->psz_name, "inputs" ) == 0 ||
strcmp( aw_child->psz_name, "options" ) == 0 ) strcmp( aw_child->psz_name, "options" ) == 0 )
{ {
i_success = asprintf( &psz_response, "%s[%s%s%s]%c%s", char *psz_recurse = recurse_answer( aw_child, psz_childdelim, 1 ),
psz_response, psz_childdelim, i_success = asprintf( &psz_tmp, "%s[%s%s%s]%c%s",
recurse_answer( aw_child, psz_response, psz_childdelim, psz_recurse,
psz_childdelim, 1 ),
psz_delim, c_comma, psz_delim ); psz_delim, c_comma, psz_delim );
free( psz_recurse );
if( i_success == -1 ) break; if( i_success == -1 ) break;
free( psz_response );
psz_response = psz_tmp;
} }
/* Not a list, so format the child as a JSON object and /* Not a list, so format the child as a JSON object and
* recurse through the child's children * recurse through the child's children
*/ */
else else
{ {
i_success = asprintf( &psz_response, "%s{%s%s%s%s}%c%s", char *psz_recurse = recurse_answer( aw_child, psz_childdelim, 0 ),
i_success = asprintf( &psz_tmp, "%s{%s%s%s%s}%c%s",
psz_response, psz_childdelim, psz_nametag, psz_response, psz_childdelim, psz_nametag,
recurse_answer( aw_child, psz_recurse, psz_delim, c_comma, psz_delim );
psz_childdelim, 0 ), free( psz_recurse );
psz_delim, c_comma, psz_delim );
if( i_success == -1 ) break; if( i_success == -1 ) break;
free( psz_response );
psz_response = psz_tmp;
} }
} }
/* Otherwise - when no children are present - the node is a /* Otherwise - when no children are present - the node is a
...@@ -307,18 +314,21 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim, ...@@ -307,18 +314,21 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
if( aw_child->psz_value == NULL if( aw_child->psz_value == NULL
|| strcmp( aw_child->psz_value, "(null)" ) == 0 ) || strcmp( aw_child->psz_value, "(null)" ) == 0 )
{ {
i_success = asprintf( &psz_response, "%snull%c%s", i_success = asprintf( &psz_tmp, "%snull%c%s",
psz_response, c_comma, psz_delim ); psz_response, c_comma, psz_delim );
if( i_success == -1 ) if( i_success == -1 ) break;
break; free( psz_response );
psz_response = psz_tmp;
} }
/* Otherwise print the value in quotation marks */ /* Otherwise print the value in quotation marks */
else else
{ {
i_success = asprintf( &psz_response, "%s\"%s\"%c%s", i_success = asprintf( &psz_tmp, "%s\"%s\"%c%s",
psz_response, aw_child->psz_value, psz_response, aw_child->psz_value,
c_comma, psz_delim ); c_comma, psz_delim );
if( i_success == -1 ) break; if( i_success == -1 ) break;
free( psz_response );
psz_response = psz_tmp;
} }
} }
/* getting next child */ /* getting next child */
...@@ -373,12 +383,13 @@ const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, ...@@ -373,12 +383,13 @@ const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
psz_delimiter = "\n"; psz_delimiter = "\n";
i_list = 1; i_list = 1;
} }
if( asprintf( &psz_response, psz_fmt, char *psz_tmp = recurse_answer( answer, psz_delimiter, i_list );
recurse_answer( answer, psz_delimiter, i_list ) ) == -1 ) if( asprintf( &psz_response, psz_fmt, psz_tmp ) == -1 )
{ {
libvlc_printerr( "Out of memory" ); libvlc_printerr( "Out of memory" );
psz_response = NULL; psz_response = NULL;
} }
free( psz_tmp );
} }
free( psz_message ); free( psz_message );
return( psz_response ); return( psz_response );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment