[vlc-devel] [RFC 14/38] demux/avi: removed usage of xmalloc

Filip Roséen filip at videolabs.io
Mon Jun 27 13:43:25 CEST 2016


This patch simply removes usage of xmalloc and replaces it with
malloc where applicable.

Some cases where xmalloc was used was actually followed by a check to
see if the allocation failed, while others immediately did a memset(
..., 0, ... ); the latter cases has been replaced by calloc.
---
 modules/demux/avi/libavi.c | 42 ++++++++++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 10 deletions(-)

diff --git a/modules/demux/avi/libavi.c b/modules/demux/avi/libavi.c
index 11fd2cb..3159e02 100644
--- a/modules/demux/avi/libavi.c
+++ b/modules/demux/avi/libavi.c
@@ -172,8 +172,11 @@ static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
     msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
     for( ; ; )
     {
-        p_chk = xmalloc( sizeof( avi_chunk_t ) );
-        memset( p_chk, 0, sizeof( avi_chunk_t ) );
+        p_chk = calloc( 1, sizeof( avi_chunk_t ) );
+
+        if( unlikely( !p_chk ) )
+            return VLC_ENOMEM;
+
         if( !p_container->common.p_first )
         {
             p_container->common.p_first = p_chk;
@@ -230,8 +233,11 @@ int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
 
     for( ; ; )
     {
-        p_chk = xmalloc( sizeof( avi_chunk_t ) );
-        memset( p_chk, 0, sizeof( avi_chunk_t ) );
+        p_chk = calloc( 0, sizeof( avi_chunk_t ) );
+
+        if( unlikely( !p_chk ) )
+            return VLC_ENOMEM;;
+
         if (unlikely( !p_riff->common.p_first ))
             p_riff->common.p_first = p_chk;
         else
@@ -397,7 +403,7 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
     {
         case( AVIFOURCC_auds ):
             p_chk->strf.auds.i_cat = AUDIO_ES;
-            p_chk->strf.auds.p_wf = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
+            p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
             if ( !p_chk->strf.auds.p_wf )
             {
                 AVI_READCHUNK_EXIT( VLC_ENOMEM );
@@ -450,7 +456,7 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
         case( AVIFOURCC_vids ):
             p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
             p_chk->strf.vids.i_cat = VIDEO_ES;
-            p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
+            p_chk->strf.vids.p_bih = malloc( __MAX( p_chk->common.i_chunk_size,
                                          sizeof( *p_chk->strf.vids.p_bih ) ) );
             if ( !p_chk->strf.vids.p_bih )
             {
@@ -544,7 +550,11 @@ static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
     }
 
     AVI_READCHUNK_ENTER;
-    p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
+    p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
+
+    if( unlikely( !p_chk->strd.p_data ) )
+        AVI_READCHUNK_EXIT( VLC_ENOMEM );
+
     memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
     AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
@@ -802,7 +812,10 @@ static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
         }
     }
     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
-    p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
+    p_strz->p_str = malloc( p_strz->i_chunk_size + 1);
+
+    if( unlikely( !p_strz->p_type || !p_strz->p_str ) )
+        goto error;
 
     if( p_strz->i_chunk_size )
     {
@@ -815,6 +828,12 @@ static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
 #endif
     AVI_READCHUNK_EXIT( VLC_SUCCESS );
+
+error:
+    free( p_strz->p_str );
+    free( p_strz->p_type );
+
+    AVI_READCHUNK_EXIT( VLC_EGENERIC );
 }
 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
 {
@@ -1076,8 +1095,11 @@ int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
 
     for( ; ; )
     {
-        p_chk = xmalloc( sizeof( avi_chunk_t ) );
-        memset( p_chk, 0, sizeof( avi_chunk_t ) );
+        p_chk = calloc( 1, sizeof( avi_chunk_t ) );
+
+        if( unlikely( !p_chk ) )
+            return VLC_ENOMEM;
+
         if( !p_root->common.p_first )
         {
             p_root->common.p_first = p_chk;
-- 
2.9.0



More information about the vlc-devel mailing list