]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/vlc.c
Dynamically allocate the dir list to prevent potential array overflows (I believe...
[vlc] / modules / misc / lua / vlc.c
index 9084739a37d31ba2f8830c38d1bd44bda4aa95ee..6c32015cc22d9438e7cfb1d9ef1a6fb00a6e6002 100644 (file)
@@ -163,8 +163,14 @@ static int file_compare( const char **a, const char **b )
 }
 
 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
-                     char **ppsz_dir_list )
+                     char ***pppsz_dir_list )
 {
+#define MAX_DIR_LIST_SIZE 5
+    *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
+    if (!*pppsz_dir_list)
+        return VLC_EGENERIC;
+    char **ppsz_dir_list = *pppsz_dir_list;
+
     int i = 0;
     char *datadir = config_GetUserDir( VLC_DATA_DIR );
 
@@ -197,6 +203,9 @@ int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
     }
 
     ppsz_dir_list[i] = NULL;
+
+    assert( i < MAX_DIR_LIST_SIZE);
+
     return VLC_SUCCESS;
 }
 
@@ -205,6 +214,7 @@ void vlclua_dir_list_free( char **ppsz_dir_list )
     char **ppsz_dir;
     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
         free( *ppsz_dir );
+    free( ppsz_dir_list );
 }
 
 /*****************************************************************************
@@ -216,9 +226,9 @@ int vlclua_scripts_batch_execute( vlc_object_t *p_this,
                                   int (*func)(vlc_object_t *, const char *, void *),
                                   void * user_data)
 {
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir_list = NULL;
 
-    int i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
+    int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
     if( i_ret != VLC_SUCCESS )
         return i_ret;
     i_ret = VLC_EGENERIC;
@@ -270,9 +280,9 @@ int vlclua_scripts_batch_execute( vlc_object_t *p_this,
 
 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
 {
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir_list = NULL;
     char **ppsz_dir;
-    vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
+    vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
     {
         for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
@@ -577,9 +587,10 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
     char **ppsz_fileend  = NULL;
     char **ppsz_file;
     char *psz_name;
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir_list = NULL;
     char **ppsz_dir;
-    vlclua_dir_list( obj, "sd", ppsz_dir_list );
+    lua_State *L = NULL;
+    vlclua_dir_list( obj, "sd", &ppsz_dir_list );
     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
     {
         int i_files;
@@ -603,52 +614,63 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
             {
                 goto error;
             }
-            FILE *fd = vlc_fopen( psz_filename, "r" );
-            if( fd )
+            L = luaL_newstate();
+            if( !L )
+            {
+                msg_Err( probe, "Could not create new Lua State" );
+                return VLC_EGENERIC;
+            }
+            luaL_openlibs( L );
+            if( vlclua_add_modules_path( probe, L, psz_filename ) )
+            {
+                msg_Err( probe, "Error while setting the module search path for %s",
+                          psz_filename );
+                goto error;
+            }
+            if( luaL_dofile( L, psz_filename ) )
             {
-                char description[256];
-                if( fgets( description, 256, fd ) != NULL )
+
+                msg_Err( probe, "Error loading script %s: %s", psz_filename,
+                          lua_tostring( L, lua_gettop( L ) ) );
+                lua_pop( L, 1 );
+                goto error;
+            }
+            char *psz_longname;
+            char *temp = strchr( *ppsz_file, '.' );
+            if( temp )
+                *temp = '\0';
+            lua_getglobal( L, "descriptor" );
+            if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
+            {
+                lua_pop( L, 1 );
+                if( !( psz_longname = strdup( *ppsz_file ) ) )
                 {
-                    char *temp = strchr( description, '\n' );
-                    if( temp )
-                        *temp = '\0';
-                    temp = strchr( *ppsz_file, '.' );
-                    if( temp )
-                        *temp = '\0';
-                    char *psz_longname;
-                    if( !strncmp( description, "--SD_Description=", 17 ) )
-                    {
-                        if( !( psz_longname = strdup( description + 17 ) ) )
-                        {
-                            fclose( fd );
-                            free( psz_filename );
-                            goto error;
-                        }
-                    }
-                    else
-                    {
-                        if( !( psz_longname = strdup( *ppsz_file ) ) )
-                        {
-                            fclose( fd );
-                            free( psz_filename );
-                            goto error;
-                        }
-                    }
-                    if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
-                                  *ppsz_file, psz_longname ) < 0 )
-                    {
-                        fclose( fd );
-                        free( psz_filename );
-                        free( psz_longname );
-                        goto error;
-                    }
-                    vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
-                    free( psz_name );
-                    free( psz_longname );
+                    free( psz_filename );
+                    goto error;
+                }
+            }
+            else
+            {
+                lua_getfield( L, -1, "title" );
+                if( !lua_isstring( L, -1 ) ||
+                    !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
+                {
+                    free( psz_filename );
+                    goto error;
                 }
-                fclose( fd );
             }
+            if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
+                          *ppsz_file, psz_longname ) < 0 )
+            {
+                free( psz_filename );
+                free( psz_longname );
+                goto error;
+            }
+            vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
+            free( psz_name );
+            free( psz_longname );
             free( psz_filename );
+            lua_close( L );
         }
     }
     if( ppsz_filelist )
@@ -668,6 +690,8 @@ error:
             free( *ppsz_file );
         free( ppsz_filelist );
     }
+    if( L )
+        lua_close( L );
     vlclua_dir_list_free( ppsz_dir_list );
     return VLC_ENOMEM;
 }
@@ -737,8 +761,8 @@ int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_
         return 1;
     }
 
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    vlclua_dir_list( obj, psz_char+1/* gruik? */, ppsz_dir_list );
+    char **ppsz_dir_list = NULL;
+    vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
     char **ppsz_dir = ppsz_dir_list;
 
     for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );