[vlc-devel] commit: Fix potential memleaks (free the module if it can't be loaded) ( Rémi Duraffort )

git version control git at videolan.org
Wed Aug 20 23:19:34 CEST 2008


vlc | branch: master | Rémi Duraffort <ivoire at videolan.org> | Wed Aug 20 21:42:15 2008 +0200| [2c28bcbadce84f19b29350939eb9cd4af294c422] | committer: Rémi Duraffort 

Fix potential memleaks (free the module if it can't be loaded)

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

 modules/audio_filter/scaletempo.c |   61 +++++++++++++++++++++----------------
 1 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/modules/audio_filter/scaletempo.c b/modules/audio_filter/scaletempo.c
index 9f7f601..08541f1 100644
--- a/modules/audio_filter/scaletempo.c
+++ b/modules/audio_filter/scaletempo.c
@@ -288,12 +288,15 @@ static int reinit_buffers( aout_filter_t *p_filter )
 
     /* overlap */
     unsigned frames_overlap = frames_stride * p->percent_overlap;
-    if( frames_overlap < 1 ) { /* if no overlap */
+    if( frames_overlap < 1 )
+    { /* if no overlap */
         p->bytes_overlap    = 0;
         p->bytes_standing   = p->bytes_stride;
         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
         p->output_overlap   = NULL;
-    } else {
+    }
+    else
+    {
         unsigned prev_overlap   = p->bytes_overlap;
         p->bytes_overlap    = frames_overlap * p->bytes_per_frame;
         p->samples_overlap  = frames_overlap * p->samples_per_frame;
@@ -301,50 +304,55 @@ static int reinit_buffers( aout_filter_t *p_filter )
         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
         p->buf_overlap      = malloc( p->bytes_overlap );
         p->table_blend      = malloc( p->samples_overlap * 4 ); /* sizeof (int32|float) */
-        if( ! p->buf_overlap || ! p->table_blend ) {
+        if( !p->buf_overlap || !p->table_blend )
             return VLC_ENOMEM;
-        }
-        if( p->bytes_overlap > prev_overlap ) {
+        if( p->bytes_overlap > prev_overlap )
             memset( (uint8_t *)p->buf_overlap + prev_overlap, 0, p->bytes_overlap - prev_overlap );
-        }
+
         float *pb = p->table_blend;
         float t = (float)frames_overlap;
-        for( i = 0; i<frames_overlap; i++ ) {
+        for( i = 0; i<frames_overlap; i++ )
+        {
             float v = i / t;
-            for( j = 0; j < p->samples_per_frame; j++ ) {
+            for( j = 0; j < p->samples_per_frame; j++ )
                 *pb++ = v;
-            }
         }
         p->output_overlap = output_overlap_float;
     }
 
     /* best overlap */
     p->frames_search = ( frames_overlap <= 1 ) ? 0 : p->ms_search * p->sample_rate / 1000.0;
-    if( p->frames_search < 1 ) { /* if no search */
+    if( p->frames_search < 1 )
+    { /* if no search */
         p->best_overlap_offset = NULL;
-    } else {
+    }
+    else
+    {
         unsigned bytes_pre_corr = ( p->samples_overlap - p->samples_per_frame ) * 4; /* sizeof (int32|float) */
         p->buf_pre_corr = malloc( bytes_pre_corr );
         p->table_window = malloc( bytes_pre_corr );
-        if( ! p->buf_pre_corr || ! p->table_window ) {
+        if( ! p->buf_pre_corr || ! p->table_window )
             return VLC_ENOMEM;
-        }
         float *pw = p->table_window;
-        for( i = 1; i<frames_overlap; i++ ) {
+        for( i = 1; i<frames_overlap; i++ )
+        {
             float v = i * ( frames_overlap - i );
-            for( j = 0; j < p->samples_per_frame; j++ ) {
+            for( j = 0; j < p->samples_per_frame; j++ )
                 *pw++ = v;
-            }
         }
         p->best_overlap_offset = best_overlap_offset_float;
     }
 
     unsigned new_size = ( p->frames_search + frames_stride + frames_overlap ) * p->bytes_per_frame;
-    if( p->bytes_queued > new_size ) {
-        if( p->bytes_to_slide > p->bytes_queued ) {
+    if( p->bytes_queued > new_size )
+    {
+        if( p->bytes_to_slide > p->bytes_queued )
+        {
           p->bytes_to_slide -= p->bytes_queued;
           p->bytes_queued    = 0;
-        } else {
+        }
+        else
+        {
             unsigned new_queued = __MIN( p->bytes_queued - p->bytes_to_slide, new_size );
             memmove( p->buf_queue,
                      p->buf_queue + p->bytes_queued - new_queued,
@@ -355,9 +363,8 @@ static int reinit_buffers( aout_filter_t *p_filter )
     }
     p->bytes_queue_max = new_size;
     p->buf_queue = malloc( p->bytes_queue_max );
-    if( ! p->buf_queue ) {
+    if( ! p->buf_queue )
         return VLC_ENOMEM;
-    }
 
     p->bytes_stride_scaled  = p->bytes_stride * p->scale;
     p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame;
@@ -400,9 +407,7 @@ static int Open( vlc_object_t *p_this )
     }
 
     if( ! b_fit )
-    {
         return VLC_EGENERIC;
-    }
 
     p_filter->pf_do_work = DoWork;
     p_filter->b_in_place = false;
@@ -410,9 +415,7 @@ static int Open( vlc_object_t *p_this )
     /* Allocate structure */
     p_sys = p_filter->p_sys = malloc( sizeof(aout_filter_sys_t) );
     if( ! p_sys )
-    {
         return VLC_ENOMEM;
-    }
 
     p_sys->scale             = 1.0;
     p_sys->sample_rate       = p_filter->input.i_rate;
@@ -446,7 +449,13 @@ static int Open( vlc_object_t *p_this )
     p_sys->bytes_queued   = 0;
     p_sys->bytes_to_slide = 0;
     p_sys->frames_stride_error = 0;
-    return reinit_buffers( p_filter );
+
+    if( reinit_buffers( p_filter ) != VLC_SUCCESS )
+    {
+        Close( p_this );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
 }
 
 static void Close( vlc_object_t *p_this )




More information about the vlc-devel mailing list