]> git.sesse.net Git - vlc/blobdiff - modules/demux/playlist/pls.c
playlist_pls: fix use after free (can be triggered by some invalid shoutcast playlist...
[vlc] / modules / demux / playlist / pls.c
index b3df8e2d8ccff1eb8b4d9c2d37f494a3e0eae9f3..20a6f52a945b7b1662f9d4861a33fe9fed09af66 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 #include <vlc_demux.h>
 
 #include "playlist.h"
@@ -44,21 +48,21 @@ static int Control( demux_t *p_demux, int i_query, va_list args );
 /*****************************************************************************
  * Import_PLS: main import function
  *****************************************************************************/
-int E_(Import_PLS)( vlc_object_t *p_this )
+int Import_PLS( vlc_object_t *p_this )
 {
     demux_t *p_demux = (demux_t *)p_this;
-    uint8_t *p_peek;
+    const uint8_t *p_peek;
     CHECK_PEEK( p_peek, 10 );
 
     if( POKE( p_peek, "[playlist]", 10 ) || POKE( p_peek, "[Reference]", 10 ) ||
-        isExtension( p_demux, ".pls" )   || isDemux( p_demux, "pls" ) )
+        demux_IsPathExtension( p_demux, ".pls" )   || demux_IsForced( p_demux, "pls" ) )
     {
         ;
     }
     else return VLC_EGENERIC;
 
     STANDARD_DEMUX_INIT_MSG(  "found valid PLS playlist file");
-    p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
+    p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
 
     return VLC_SUCCESS;
 }
@@ -66,13 +70,10 @@ int E_(Import_PLS)( vlc_object_t *p_this )
 /*****************************************************************************
  * Deactivate: frees unused data
  *****************************************************************************/
-void E_(Close_PLS)( vlc_object_t *p_this )
+void Close_PLS( vlc_object_t *p_this )
 {
     demux_t *p_demux = (demux_t *)p_this;
-    if( p_demux->p_sys->psz_prefix )
-    {
-        free( p_demux->p_sys->psz_prefix );
-    }
+    free( p_demux->p_sys->psz_prefix );
     free( p_demux->p_sys );
 }
 
@@ -86,11 +87,11 @@ static int Demux( demux_t *p_demux )
     char          *psz_key;
     char          *psz_value;
     int            i_item = -1;
-    int            i_new_item = 0;
-    int            i_key_length;
     input_item_t *p_input;
 
-    INIT_PLAYLIST_STUFF;
+    input_item_t *p_current_input = GetCurrentItem(p_demux);
+
+    input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
 
     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
     {
@@ -109,7 +110,6 @@ static int Demux( demux_t *p_demux )
         }
         else
         {
-            msg_Warn( p_demux, "invalid line in pls file" );
             free( psz_line );
             continue;
         }
@@ -121,68 +121,50 @@ static int Demux( demux_t *p_demux )
         }
         if( !strcasecmp( psz_key, "numberofentries" ) )
         {
-            msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) ); 
+            msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) );
             free( psz_line);
             continue;
         }
+
         /* find the number part of of file1, title1 or length1 etc */
-        i_key_length = strlen( psz_key );
-        if( i_key_length >= 4 ) /* Ref1 type case */
-        {
-            i_new_item = atoi( psz_key + 3 );
-            if( i_new_item == 0 && i_key_length >= 5 ) /* file1 type case */
-            {
-                i_new_item = atoi( psz_key + 4 );
-                if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
-                {
-                    i_new_item = atoi( psz_key + 5 );
-                    if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
-                    {
-                        i_new_item = atoi( psz_key + 6 );
-                    }
-                }
-            }
-        }
-        if( i_new_item == 0 )
+        int i_new_item;
+        if( sscanf( psz_key, "%*[^0-9]%d", &i_new_item ) != 1 )
         {
             msg_Warn( p_demux, "couldn't find number of items" );
             free( psz_line );
             continue;
         }
+
         if( i_item == -1 )
-        {
             i_item = i_new_item;
-        }
-        /* we found a new item, insert the previous */
-        if( i_item != i_new_item )
+        else if( i_item != i_new_item )
         {
+            /* we found a new item, insert the previous */
             if( psz_mrl )
             {
-                p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
-                                            0, NULL, -1 );
-                input_ItemCopyOptions( p_current->p_input, p_input );
-                playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
-                                       PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
-                                       PLAYLIST_END, NULL, NULL, VLC_FALSE );
+                p_input = input_item_New( p_demux, psz_mrl, psz_name );
+                input_item_CopyOptions( p_current_input, p_input );
+                input_item_node_AppendItem( p_subitems, p_input );
+                vlc_gc_decref( p_input );
+                free( psz_mrl_orig );
+                psz_mrl_orig = psz_mrl = NULL;
             }
             else
             {
                 msg_Warn( p_demux, "no file= part found for item %d", i_item );
             }
-            if( psz_name )
-            {
-                free( psz_name );
-                psz_name = NULL;
-            }
+            free( psz_name );
+            psz_name = NULL;
             i_duration = -1;
             i_item = i_new_item;
-            i_new_item = 0;
         }
+
         if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) ||
             !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
         {
+            free( psz_mrl_orig );
             psz_mrl_orig =
-            psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
+            psz_mrl = ProcessMRL( psz_value, p_demux->p_sys->psz_prefix );
 
             if( !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
             {
@@ -197,11 +179,12 @@ static int Demux( demux_t *p_demux )
         }
         else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
         {
+            free( psz_name );
             psz_name = strdup( psz_value );
         }
         else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
         {
-            i_duration = atoi( psz_value );
+            i_duration = atoll( psz_value );
             if( i_duration != -1 )
             {
                 i_duration *= 1000000;
@@ -216,29 +199,27 @@ static int Demux( demux_t *p_demux )
     /* Add last object */
     if( psz_mrl )
     {
-        p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,0, NULL, -1 );
-        input_ItemCopyOptions( p_current->p_input, p_input );
-        playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
-                               PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
-                               PLAYLIST_END, NULL, NULL, VLC_FALSE );
+        p_input = input_item_New( p_demux, psz_mrl, psz_name );
+        input_item_CopyOptions( p_current_input, p_input );
+        input_item_node_AppendItem( p_subitems, p_input );
+        vlc_gc_decref( p_input );
         free( psz_mrl_orig );
-        psz_mrl = NULL;
     }
     else
     {
         msg_Warn( p_demux, "no file= part found for item %d", i_item );
     }
-    if( psz_name )
-    {
-        free( psz_name );
-        psz_name = NULL;
-    }
+    free( psz_name );
+    psz_name = NULL;
+
+    input_item_node_PostAndDelete( p_subitems );
 
-    HANDLE_PLAY_AND_RELEASE;
-    return -1; /* Needed for correct operation of go back */
+    vlc_gc_decref(p_current_input);
+    return 0; /* Needed for correct operation of go back */
 }
 
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
+    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
     return VLC_EGENERIC;
 }