[vlc-commits] lua: fix handling of removed item

Rémi Denis-Courmont git at videolan.org
Mon May 15 18:24:05 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon May 15 19:23:03 2017 +0300| [5616dbd1bbbfe27aae9aa12b108359608d0133e6] | committer: Rémi Denis-Courmont

lua: fix handling of removed item

Removed an already removed item is a scripting error.
Garbage collecting an already removed item is a normal operation.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5616dbd1bbbfe27aae9aa12b108359608d0133e6
---

 modules/lua/libs/sd.c | 65 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/modules/lua/libs/sd.c b/modules/lua/libs/sd.c
index 9a59033d25..e0e3f91f8a 100644
--- a/modules/lua/libs/sd.c
+++ b/modules/lua/libs/sd.c
@@ -44,20 +44,45 @@
 #include "../vlc.h"
 #include "../libs.h"
 
+static int vlclua_sd_delete_common( input_item_t **pp_item )
+{
+    assert(pp_item != NULL);
 
-/*** Input item ***/
+    input_item_t *p_item = *pp_item;
+    if (p_item != NULL) /* item may be NULL if already removed earlier */
+        input_item_Release( p_item );
 
-static int vlclua_sd_item_delete( lua_State *L )
+    return 1;
+}
+
+static int vlclua_sd_remove_common( lua_State *L, input_item_t **pp_item )
 {
-    input_item_t **pp_item = luaL_checkudata( L, 1, "input_item_t" );
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+
+    if (pp_item == NULL)
+        return luaL_error( L, "expected item" );
+
     input_item_t *p_item = *pp_item;
+    if (*pp_item == NULL)
+        return luaL_error( L, "already removed item" );
 
-    assert( p_item != NULL );
+    services_discovery_RemoveItem( p_sd, p_item );
     input_item_Release( p_item );
+    /* Make sure we won't try to remove it again */
     *pp_item = NULL;
     return 1;
 }
 
+
+/*** Input item ***/
+
+static int vlclua_sd_item_delete( lua_State *L )
+{
+    input_item_t **pp_item = luaL_checkudata( L, 1, "input_item_t" );
+
+    return vlclua_sd_delete_common( pp_item );
+}
+
 #define vlclua_item_luareg( a ) \
 { "set_" # a, vlclua_item_set_ ## a },
 
@@ -229,12 +254,8 @@ static input_item_t *vlclua_sd_create_item( services_discovery_t *p_sd,
 static int vlclua_sd_node_delete( lua_State *L )
 {
     input_item_t **pp_item = luaL_checkudata( L, 1, "node" );
-    input_item_t *p_item = *pp_item;
 
-    assert( p_item != NULL );
-    input_item_Release( p_item );
-    *pp_item = NULL;
-    return 1;
+    return vlclua_sd_delete_common( pp_item );
 }
 
 static int vlclua_sd_add_sub_common( services_discovery_t *p_sd,
@@ -351,30 +372,16 @@ static int vlclua_sd_add_node( lua_State *L )
 
 static int vlclua_sd_remove_item( lua_State *L )
 {
-    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
-    if( !lua_isnil( L, 1 ) )
-    {
-        input_item_t **pp_input = luaL_checkudata( L, 1, "input_item_t" );
-        if( *pp_input )
-            services_discovery_RemoveItem( p_sd, *pp_input );
-        /* Make sure we won't try to remove it again */
-        *pp_input = NULL;
-    }
-    return 1;
+    input_item_t **pp_input = luaL_checkudata( L, 1, "input_item_t" );
+
+    return vlclua_sd_remove_common( L, pp_input );
 }
 
 static int vlclua_sd_remove_node( lua_State *L )
 {
-    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
-    if( !lua_isnil( L, 1 ) )
-    {
-        input_item_t **pp_input = luaL_checkudata( L, 1, "node" );
-        if( *pp_input )
-            services_discovery_RemoveItem( p_sd, *pp_input );
-        /* Make sure we won't try to remove it again */
-        *pp_input = NULL;
-    }
-    return 1;
+    input_item_t **pp_input = luaL_checkudata( L, 1, "node" );
+
+    return vlclua_sd_remove_common( L, pp_input );
 }
 
 static const luaL_Reg vlclua_sd_sd_reg[] = {



More information about the vlc-commits mailing list