]> git.sesse.net Git - vlc/commitdiff
vlm: fix memory leaks.
authorRémi Duraffort <ivoire@videolan.org>
Mon, 21 Mar 2011 20:44:11 +0000 (21:44 +0100)
committerRémi Duraffort <ivoire@videolan.org>
Mon, 21 Mar 2011 20:44:42 +0000 (21:44 +0100)
src/control/vlm.c

index 81ba9fc40a653e58a3e0868d2d55d193090afd31..c6d5f368107480c99f11d997353aea497d3f871e 100644 (file)
@@ -227,6 +227,7 @@ static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
     char* psz_childdelim = NULL;
     char* psz_nametag = NULL;
     char* psz_response = strdup( "" );
+    char *psz_tmp;
     int i_success = 0;
     int i;
     vlm_message_t *aw_child, **paw_child;
@@ -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 */
         if( !i_list )
         {
-            i_success = asprintf( &psz_response, "%s\"%s\": ",
+            i_success = asprintf( &psz_tmp, "%s\"%s\": ",
                           psz_response, aw_child->psz_name );
             if( i_success == -1 ) break;
+            free( psz_response );
+            psz_response = psz_tmp;
         }
 
         /* If child node has children, */
@@ -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, "options" ) == 0 )
             {
-                i_success = asprintf( &psz_response, "%s[%s%s%s]%c%s",
-                                      psz_response, psz_childdelim,
-                                      recurse_answer( aw_child,
-                                                      psz_childdelim, 1 ),
+                char *psz_recurse = recurse_answer( aw_child, psz_childdelim, 1 ),
+                i_success = asprintf( &psz_tmp, "%s[%s%s%s]%c%s",
+                                      psz_response, psz_childdelim, psz_recurse,
                                       psz_delim, c_comma, psz_delim );
+                free( psz_recurse );
                 if( i_success == -1 ) break;
+                free( psz_response );
+                psz_response = psz_tmp;
             }
             /* Not a list, so format the child as a JSON object and
              * recurse through the child's children
              */
             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,
-                                      recurse_answer( aw_child,
-                                                      psz_childdelim, 0 ),
-                                      psz_delim, c_comma, psz_delim );
+                                      psz_recurse, psz_delim, c_comma, psz_delim );
+                free( psz_recurse );
                 if( i_success == -1 ) break;
+                free( psz_response );
+                psz_response = psz_tmp;
             }
         }
         /* 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,
             if( aw_child->psz_value == NULL
                 || 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 );
-                if( i_success == -1 )
-                    break;
+                if( i_success == -1 ) break;
+                free( psz_response );
+                psz_response = psz_tmp;
             }
             /* Otherwise print the value in quotation marks */
             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,
                                       c_comma, psz_delim );
                 if( i_success == -1 ) break;
+                free( psz_response );
+                psz_response = psz_tmp;
             }
         }
         /* getting next child */
@@ -373,12 +383,13 @@ const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
             psz_delimiter = "\n";
             i_list = 1;
         }
-        if( asprintf( &psz_response, psz_fmt,
-                      recurse_answer( answer, psz_delimiter, i_list ) ) == -1 )
+        char *psz_tmp = recurse_answer( answer, psz_delimiter, i_list );
+        if( asprintf( &psz_response, psz_fmt, psz_tmp ) == -1 )
         {
             libvlc_printerr( "Out of memory" );
             psz_response = NULL;
         }
+        free( psz_tmp );
     }
     free( psz_message );
     return( psz_response );