]> git.sesse.net Git - vlc/blobdiff - src/control/media_list.c
fix for #1533: check on array boundaries before removing media
[vlc] / src / control / media_list.c
index 8463fb18f07167724fad7d4a9f83d82bd98619f4..4286fb942288cc536fe43eb146042fc4b742bc1e 100644 (file)
 #include <assert.h>
 #include "vlc_arrays.h"
 
+typedef enum EventPlaceInTime {
+    EventWillHappen,
+    EventDidHappen
+} EventPlaceInTime;
+
+//#define DEBUG_MEDIA_LIST
+
+#ifdef DEBUG_MEDIA_LIST
+# define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
+#else
+# define trace( ... )
+#endif
+
 /*
  * Private functions
  */
 static void
 notify_item_addition( libvlc_media_list_t * p_mlist,
                       libvlc_media_descriptor_t * p_md,
-                      int index )
+                      int index,
+                      EventPlaceInTime event_status )
 {
     libvlc_event_t event;
 
     /* Construct the event */
-    event.type = libvlc_MediaListItemAdded;
-    event.u.media_list_item_added.item = p_md;
-    event.u.media_list_item_added.index = index;
+    if( event_status == EventDidHappen )
+    {
+        trace("item was added at index %d\n", index);
+        event.type = libvlc_MediaListItemAdded;
+        event.u.media_list_item_added.item = p_md;
+        event.u.media_list_item_added.index = index;
+    }
+    else /* if( event_status == EventWillHappen ) */
+    {
+        event.type = libvlc_MediaListWillAddItem;
+        event.u.media_list_will_add_item.item = p_md;
+        event.u.media_list_will_add_item.index = index;
+    }
 
     /* Send the event */
     libvlc_event_send( p_mlist->p_event_manager, &event );
@@ -61,14 +85,25 @@ notify_item_addition( libvlc_media_list_t * p_mlist,
 static void
 notify_item_deletion( libvlc_media_list_t * p_mlist,
                       libvlc_media_descriptor_t * p_md,
-                      int index )
+                      int index,
+                      EventPlaceInTime event_status )
 {
     libvlc_event_t event;
 
     /* Construct the event */
-    event.type = libvlc_MediaListItemDeleted;
-    event.u.media_list_item_deleted.item = p_md;
-    event.u.media_list_item_deleted.index = index;
+    if( event_status == EventDidHappen )
+    {
+        trace("item at index %d was deleted\n", index);
+        event.type = libvlc_MediaListItemDeleted;
+        event.u.media_list_item_deleted.item = p_md;
+        event.u.media_list_item_deleted.index = index;
+    }
+    else /* if( event_status == EventWillHappen ) */
+    {
+        event.type = libvlc_MediaListWillDeleteItem;
+        event.u.media_list_will_delete_item.item = p_md;
+        event.u.media_list_will_delete_item.index = index;
+    }
 
     /* Send the event */
     libvlc_event_send( p_mlist->p_event_manager, &event );
@@ -99,11 +134,16 @@ libvlc_media_list_new( libvlc_instance_t * p_inst,
 
     /* Code for that one should be handled in flat_media_list.c */
     p_mlist->p_flat_mlist = NULL;
+    p_mlist->b_read_only = VLC_FALSE;
 
     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
             libvlc_MediaListItemAdded, p_e );
+    libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
+            libvlc_MediaListWillAddItem, p_e );
     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
             libvlc_MediaListItemDeleted, p_e );
+    libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
+            libvlc_MediaListWillDeleteItem, p_e );
 
     if( libvlc_exception_raised( p_e ) )
     {
@@ -274,11 +314,29 @@ void libvlc_media_list_add_media_descriptor(
                                    libvlc_media_list_t * p_mlist,
                                    libvlc_media_descriptor_t * p_md,
                                    libvlc_exception_t * p_e )
+{
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+
+    _libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
+}
+
+/* LibVLC internal version */
+void _libvlc_media_list_add_media_descriptor(
+                                   libvlc_media_list_t * p_mlist,
+                                   libvlc_media_descriptor_t * p_md,
+                                   libvlc_exception_t * p_e )
 {
     (void)p_e;
     libvlc_media_descriptor_retain( p_md );
+
+    notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
     vlc_array_append( &p_mlist->items, p_md );
-    notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1 );
+    notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
 }
 
 /**************************************************************************
@@ -291,12 +349,29 @@ void libvlc_media_list_insert_media_descriptor(
                                    libvlc_media_descriptor_t * p_md,
                                    int index,
                                    libvlc_exception_t * p_e )
+{
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+    _libvlc_media_list_insert_media_descriptor( p_mlist, p_md, index, p_e );
+}
+
+/* LibVLC internal version */
+void _libvlc_media_list_insert_media_descriptor(
+                                   libvlc_media_list_t * p_mlist,
+                                   libvlc_media_descriptor_t * p_md,
+                                   int index,
+                                   libvlc_exception_t * p_e )
 {
     (void)p_e;
     libvlc_media_descriptor_retain( p_md );
 
+    notify_item_addition( p_mlist, p_md, index, EventWillHappen );
     vlc_array_insert( &p_mlist->items, p_md, index );
-    notify_item_addition( p_mlist, p_md, index );
+    notify_item_addition( p_mlist, p_md, index, EventDidHappen );
 }
 
 /**************************************************************************
@@ -308,12 +383,34 @@ void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
                                      int index,
                                      libvlc_exception_t * p_e )
 {
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+    _libvlc_media_list_remove_index( p_mlist, index, p_e );
+}
+
+/* LibVLC internal version */
+void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
+                                     int index,
+                                     libvlc_exception_t * p_e )
+{
+
     libvlc_media_descriptor_t * p_md;
 
+    if( index < 0 || index > vlc_array_count( &p_mlist->items ))
+    {
+        libvlc_exception_raise( p_e, "Index out of bounds exception");
+        return;
+    }
+            
     p_md = vlc_array_item_at_index( &p_mlist->items, index );
 
+    notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
     vlc_array_remove( &p_mlist->items, index );
-    notify_item_deletion( p_mlist, p_md, index );
+    notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
 
     libvlc_media_descriptor_release( p_md );
 }
@@ -328,6 +425,8 @@ libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
                                  int index,
                                  libvlc_exception_t * p_e )
 {
+    VLC_UNUSED(p_e);
+
     libvlc_media_descriptor_t * p_md;
     p_md = vlc_array_item_at_index( &p_mlist->items, index );
     libvlc_media_descriptor_retain( p_md );
@@ -344,6 +443,8 @@ int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
                                      libvlc_media_descriptor_t * p_searched_md,
                                      libvlc_exception_t * p_e )
 {
+    VLC_UNUSED(p_e);
+
     libvlc_media_descriptor_t * p_md;
     int i;
     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
@@ -355,6 +456,16 @@ int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
     return -1;
 }
 
+/**************************************************************************
+ *       libvlc_media_list_is_readonly (Public)
+ *
+ * This indicates if this media list is read-only from a user point of view
+ **************************************************************************/
+int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
+{
+    return p_mlist->b_read_only;
+}
+
 /**************************************************************************
  *       libvlc_media_list_lock (Public)
  *
@@ -378,7 +489,6 @@ void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
 }
 
 
-
 /**************************************************************************
  *       libvlc_media_list_p_event_manager (Public)
  *