[vlc-commits] lua: fix memory and object leak and reset locale on error path

Pierre Ynard git at videolan.org
Mon May 21 01:00:19 CEST 2012


vlc/vlc-2.0 | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Sun May 20 03:48:08 2012 +0200| [f8640237cbc164d311f1c3f7212e86c033a6bca6] | committer: Pierre Ynard

lua: fix memory and object leak and reset locale on error path
(cherry picked from commit 31b5fbdb6d43462db5de00f13e910e0d9441ce3e)

Signed-off-by: Pierre Ynard <linkfanel at yahoo.fr>

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

 modules/lua/libs/equalizer.c |   28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/modules/lua/libs/equalizer.c b/modules/lua/libs/equalizer.c
index 75b2334..a800d02 100644
--- a/modules/lua/libs/equalizer.c
+++ b/modules/lua/libs/equalizer.c
@@ -136,7 +136,7 @@ Band 9: 16 kHz
 *****************************************************************************/
 static int vlclua_equalizer_get( lua_State *L )
 {
-    int bands = 9;
+    const unsigned bands = 10;
     input_thread_t *p_input = vlclua_get_input_internal( L );
     if( !p_input )
         return 0;
@@ -145,7 +145,6 @@ static int vlclua_equalizer_get( lua_State *L )
     if( !p_aout )
         return 0;
 
-    float level = 0 ;
     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
     if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
     {
@@ -162,24 +161,31 @@ static int vlclua_equalizer_get( lua_State *L )
         vlc_object_release( p_aout );
         return 0;
     }
+
+    bool error = false;
     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
     locale_t oldloc = uselocale (loc);
-    int i = 0;
-    char *str;
     lua_newtable( L );
-    while( bands >= 0 )
+    for( unsigned i = 0; i < bands; i++ )
     {
-        level = strtof( psz_bands, &psz_bands);
-        bands--;
+        float level = strtof( psz_bands, &psz_bands );
+        char *str;
         if( asprintf( &str , "%f" , level ) == -1 )
-            return 0;
+        {
+            error = true;
+            break;
+        }
         lua_pushstring( L, str );
         free(str);
-        if( asprintf( &str , "band id=\"%d\"", i++ ) == -1 )
-            return 0;
+        if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
+        {
+            error = true;
+            break;
+        }
         lua_setfield( L , -2 , str );
         free( str );
     }
+
     free( psz_bands_origin );
     if( loc != (locale_t)0 )
     {
@@ -187,7 +193,7 @@ static int vlclua_equalizer_get( lua_State *L )
         freelocale (loc);
     }
     vlc_object_release( p_aout );
-    return 1;
+    return error ? 0 : 1;
 }
 
 



More information about the vlc-commits mailing list