]> git.sesse.net Git - vlc/blobdiff - modules/demux/asf/libasf.c
Merge branch 'base' into master
[vlc] / modules / demux / asf / libasf.c
index 3eaee83a2f0a6c75b6f02b2bbf886913c3aa3468..46183cb4c6b5d37b4e231a736fbc62edfa8cdfc1 100644 (file)
@@ -154,7 +154,7 @@ bool ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
  ****************************************************************************/
 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
+    asf_object_common_t *p_common = &p_obj->common;
     const uint8_t *p_peek;
 
     if( stream_Peek( s, &p_peek, 24 ) < 24 )
@@ -213,7 +213,7 @@ static void ASF_FreeObject_Null( asf_object_t *pp_obj )
 
 static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
+    asf_object_header_t *p_hdr = &p_obj->header;
     asf_object_t        *p_subobj;
     int                 i_peek;
     const uint8_t       *p_peek;
@@ -243,7 +243,7 @@ static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
     {
         p_subobj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
+        if( !p_subobj || ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
         {
             free( p_subobj );
             break;
@@ -256,7 +256,7 @@ static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
 
 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
+    asf_object_data_t *p_data = &p_obj->data;
     int               i_peek;
     const uint8_t     *p_peek;
 
@@ -281,13 +281,13 @@ static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
 
 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
+    asf_object_index_t *p_index = &p_obj->index;
     const uint8_t      *p_peek;
-    int                 i;
+    unsigned int       i;
 
     /* We just ignore error on the index */
     if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
-        __MAX( (int)p_index->i_object_size, 56 ) )
+        __MAX( (int64_t)p_index->i_object_size, 56 ) )
         return VLC_SUCCESS;
 
     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
@@ -313,9 +313,10 @@ static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
 
     p_index->index_entry = calloc( p_index->i_index_entry_count,
                                    sizeof(asf_index_entry_t) );
+    if( !p_index->index_entry )
+        return VLC_ENOMEM;
 
-    for( i = 0, p_peek += 56; i < (int)p_index->i_index_entry_count;
-         i++, p_peek += 6 )
+    for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 )
     {
         p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
         p_index->index_entry[i].i_packet_count = GetDWLE( p_peek + 4 );
@@ -326,14 +327,14 @@ static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_Index( asf_object_t *p_obj )
 {
-    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
+    asf_object_index_t *p_index = &p_obj->index;
 
     FREENULL( p_index->index_entry );
 }
 
 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
+    asf_object_file_properties_t *p_fp = &p_obj->file_properties;
     int           i_peek;
     const uint8_t *p_peek;
 
@@ -372,8 +373,7 @@ static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
 {
-    asf_object_metadata_t *p_meta =
-        (asf_object_metadata_t *)p_obj;
+    asf_object_metadata_t *p_meta = &p_obj->metadata;
     unsigned int i;
 
     for( i = 0; i < p_meta->i_record_entries_count; i++ )
@@ -386,8 +386,7 @@ static void ASF_FreeObject_metadata( asf_object_t *p_obj )
 
 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_metadata_t *p_meta =
-        (asf_object_metadata_t *)p_obj;
+    asf_object_metadata_t *p_meta = &p_obj->metadata;
 
     int i_peek;
     unsigned int i;
@@ -397,7 +396,7 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 #endif
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
-        __MAX( (int)p_meta->i_object_size, 26 ) )
+        __MAX( (int64_t)p_meta->i_object_size, 26 ) )
        return VLC_EGENERIC;
 
     p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
@@ -406,6 +405,8 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 
     p_meta->record = calloc( p_meta->i_record_entries_count,
                              sizeof(asf_metadata_record_t) );
+    if( !p_meta->record )
+        return VLC_ENOMEM;
 
     for( i = 0; i < p_meta->i_record_entries_count; i++ )
     {
@@ -440,7 +441,7 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
         {
             p_record->p_data = malloc( i_data );
             p_record->i_data = i_data;
-            if( i_data > 0 )
+            if( p_record->p_data && i_data > 0 )
                 memcpy( p_record->p_data, p_data, i_data );
 
             p_data += i_data;
@@ -494,8 +495,7 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 
 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_header_extension_t *p_he =
-        (asf_object_header_extension_t *)p_obj;
+    asf_object_header_extension_t *p_he = &p_obj->header_extension;
     int     i_peek;
     const uint8_t *p_peek;
 
@@ -508,7 +508,7 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
     p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
     if( p_he->i_header_extension_size )
     {
-        if( i_peek-46 < (int)p_he->i_header_extension_size )
+        if( (unsigned int)(i_peek-46) < p_he->i_header_extension_size )
             return VLC_EGENERIC;
 
         p_he->p_header_extension_data =
@@ -540,7 +540,7 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
     {
         asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
+        if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
         {
             free( p_obj );
             break;
@@ -557,16 +557,14 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
 {
-    asf_object_header_extension_t *p_he =
-        (asf_object_header_extension_t *)p_obj;
+    asf_object_header_extension_t *p_he = &p_obj->header_extension;
 
     FREENULL( p_he->p_header_extension_data );
 }
 
 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_stream_properties_t *p_sp =
-                    (asf_object_stream_properties_t*)p_obj;
+    asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
     size_t        i_peek;
     const uint8_t *p_peek;
 
@@ -590,17 +588,13 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
         p_sp->p_type_specific_data =
             malloc( p_sp->i_type_specific_data_length );
-        if( p_sp->p_type_specific_data == NULL )
+        if( !p_sp->p_type_specific_data )
             return VLC_ENOMEM;
 
         memcpy( p_sp->p_type_specific_data, p_peek + 78,
                 p_sp->i_type_specific_data_length );
         i_peek -= p_sp->i_type_specific_data_length;
     }
-    else
-    {
-        p_sp->p_type_specific_data = NULL;
-    }
 
     if( p_sp->i_error_correction_data_length )
     {
@@ -612,7 +606,7 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
         p_sp->p_error_correction_data =
             malloc( p_sp->i_error_correction_data_length );
-        if( p_sp->p_error_correction_data == NULL )
+        if( !p_sp->p_error_correction_data )
         {
             free( p_sp->p_type_specific_data );
             return VLC_ENOMEM;
@@ -621,10 +615,6 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
                 p_peek + 78 + p_sp->i_type_specific_data_length,
                 p_sp->i_error_correction_data_length );
     }
-    else
-    {
-        p_sp->p_error_correction_data = NULL;
-    }
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
@@ -646,8 +636,7 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
 {
-    asf_object_stream_properties_t *p_sp =
-                (asf_object_stream_properties_t*)p_obj;
+    asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
 
     FREENULL( p_sp->p_type_specific_data );
     FREENULL( p_sp->p_error_correction_data );
@@ -656,7 +645,7 @@ static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
 
 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
+    asf_object_codec_list_t *p_cl = &p_obj->codec_list;
     int     i_peek;
     const uint8_t *p_peek, *p_data;
 
@@ -701,13 +690,12 @@ static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
             if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
             {
                 p_codec->p_information = malloc( p_codec->i_information_length );
-                memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
+                if( p_codec->p_information )
+                    memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
+                else
+                    p_codec->i_information_length = 0;
                 p_data += p_codec->i_information_length;
             }
-            else
-            {
-                p_codec->p_information = NULL;
-            }
         }
         p_cl->i_codec_entries_count = i_codec;
     }
@@ -736,7 +724,7 @@ static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
 {
-    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
+    asf_object_codec_list_t *p_cl = &p_obj->codec_list;
     unsigned int i_codec;
 
     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
@@ -754,8 +742,7 @@ static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
  * and for the some others object, length give char16 count ... */
 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
 {
-    asf_object_content_description_t *p_cd =
-        (asf_object_content_description_t *)p_obj;
+    asf_object_content_description_t *p_cd = &p_obj->content_description;
     const uint8_t *p_peek, *p_data;
     int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
     vlc_iconv_t cd = (vlc_iconv_t)-1;
@@ -774,14 +761,16 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
     }
 
 /* FIXME i_size*3 is the worst case. */
-#define GETSTRINGW( psz_str, i_size ) \
-    psz_str = (char *)calloc( i_size*3+1, sizeof( char ) ); \
-    ib = (const char *)p_data; \
-    ob = psz_str; \
-    i_ibl = i_size; \
-    i_obl = i_size*3; \
-    i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
-    p_data += i_size;
+#define GETSTRINGW( psz_str, i_size ) do { \
+    psz_str = calloc( i_size*3+1, sizeof(char) ); \
+    if( psz_str ) { \
+        ib = (const char *)p_data; \
+        ob = psz_str; \
+        i_ibl = i_size; \
+        i_obl = i_size*3; \
+        i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
+        p_data += i_size; \
+    } } while(0)
 
     p_data = p_peek + 24;
     
@@ -821,8 +810,7 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
 
 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
 {
-    asf_object_content_description_t *p_cd =
-        (asf_object_content_description_t *)p_obj;
+    asf_object_content_description_t *p_cd = &p_obj->content_description;
 
     FREENULL( p_cd->psz_title );
     FREENULL( p_cd->psz_artist );
@@ -834,8 +822,7 @@ static void ASF_FreeObject_content_description( asf_object_t *p_obj)
 /* Language list: */
 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
 {
-    asf_object_language_list_t *p_ll =
-        (asf_object_language_list_t*)p_obj;
+    asf_object_language_list_t *p_ll = &p_obj->language_list;
     const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
@@ -849,6 +836,8 @@ static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
     if( p_ll->i_language > 0 )
     {
         p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
+        if( !p_ll->ppsz_language )
+            return VLC_ENOMEM;
 
         for( i = 0; i < p_ll->i_language; i++ )
         {
@@ -871,8 +860,7 @@ static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
 
 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
 {
-    asf_object_language_list_t *p_ll =
-        (asf_object_language_list_t *)p_obj;
+    asf_object_language_list_t *p_ll = &p_obj->language_list;
     int i;
 
     for( i = 0; i < p_ll->i_language; i++ )
@@ -884,8 +872,7 @@ static void ASF_FreeObject_language_list( asf_object_t *p_obj)
 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
                                                      asf_object_t *p_obj)
 {
-    asf_object_stream_bitrate_properties_t *p_sb =
-        (asf_object_stream_bitrate_properties_t *)p_obj;
+    asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
     const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
@@ -926,8 +913,7 @@ static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
                                                       asf_object_t *p_obj)
 {
-    asf_object_extended_stream_properties_t *p_esp =
-        (asf_object_extended_stream_properties_t*)p_obj;
+    asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
     const uint8_t *p_peek, *p_data;
     int i_peek, i;
 
@@ -958,6 +944,13 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
                                              sizeof(int) );
     p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
                                       sizeof(char*) );
+    if( !p_esp->pi_stream_name_language ||
+        !p_esp->ppsz_stream_name )
+    {
+        free( p_esp->pi_stream_name_language );
+        free( p_esp->ppsz_stream_name );
+        return VLC_ENOMEM;
+    }
     for( i = 0; i < p_esp->i_stream_name_count; i++ )
     {
         if( !ASF_HAVE( 2+2 ) )
@@ -983,7 +976,7 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
  
         p_sp = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_sp, NULL ) )
+        if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
         {
             free( p_sp );
         }
@@ -1024,8 +1017,7 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
 }
 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
 {
-    asf_object_extended_stream_properties_t *p_esp =
-        (asf_object_extended_stream_properties_t *)p_obj;
+    asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
     int i;
 
     for( i = 0; i < p_esp->i_stream_name_count; i++ )
@@ -1038,8 +1030,7 @@ static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
                                                      asf_object_t *p_obj)
 {
-    asf_object_advanced_mutual_exclusion_t *p_ae =
-        (asf_object_advanced_mutual_exclusion_t *)p_obj;
+    asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
     const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
@@ -1071,8 +1062,7 @@ static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
 }
 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
 {
-    asf_object_advanced_mutual_exclusion_t *p_ae =
-        (asf_object_advanced_mutual_exclusion_t *)p_obj;
+    asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
 
     FREENULL( p_ae->pi_stream_number );
 }
@@ -1081,8 +1071,7 @@ static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
 static int ASF_ReadObject_stream_prioritization( stream_t *s,
                                                  asf_object_t *p_obj)
 {
-    asf_object_stream_prioritization_t *p_sp =
-        (asf_object_stream_prioritization_t *)p_obj;
+    asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
     const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
@@ -1098,6 +1087,13 @@ static int ASF_ReadObject_stream_prioritization( stream_t *s,
     p_sp->pi_priority_stream_number =
                              calloc( p_sp->i_priority_count, sizeof(int) );
 
+    if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
+    {
+        free( p_sp->pi_priority_flag );
+        free( p_sp->pi_priority_stream_number );
+        return VLC_ENOMEM;
+    }
+
     for( i = 0; i < p_sp->i_priority_count; i++ )
     {
         if( !ASF_HAVE(2+2) )
@@ -1118,8 +1114,7 @@ static int ASF_ReadObject_stream_prioritization( stream_t *s,
 }
 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
 {
-    asf_object_stream_prioritization_t *p_sp =
-        (asf_object_stream_prioritization_t *)p_obj;
+    asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
 
     FREENULL( p_sp->pi_priority_stream_number );
     FREENULL( p_sp->pi_priority_flag );
@@ -1130,7 +1125,7 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
                                                         asf_object_t *p_obj)
 {
     asf_object_extended_content_description_t *p_ec =
-        (asf_object_extended_content_description_t *)p_obj;
+                                        &p_obj->extended_content_description;
     const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
@@ -1174,13 +1169,17 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
             int j;
 
             p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
-            for( j = 0; j < i_size; j++ )
+            if( p_ec->ppsz_value[i] )
             {
-                const uint8_t v = ASF_READ1();
-                p_ec->ppsz_value[i][2*j+0] = hex[v>>4];
-                p_ec->ppsz_value[i][2*j+1] = hex[v&0xf];
+                char *psz_value = p_ec->ppsz_value[i];
+                for( j = 0; j < i_size; j++ )
+                {
+                    const uint8_t v = ASF_READ1();
+                    psz_value[2*j+0] = hex[v>>4];
+                    psz_value[2*j+1] = hex[v&0xf];
+                }
+                psz_value[2*i_size] = '\0';
             }
-            p_ec->ppsz_value[i][2*i_size] = '\0';
         }
         else if( i_type == 2 )
         {
@@ -1191,17 +1190,20 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
         else if( i_type == 3 )
         {
             /* DWord */
-            asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() );
+            if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else if( i_type == 4 )
         {
             /* QWord */
-            asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() );
+            if( asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else if( i_type == 5 )
         {
             /* Word */
-            asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() );
+            if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else
         {
@@ -1223,7 +1225,7 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
 {
     asf_object_extended_content_description_t *p_ec =
-        (asf_object_extended_content_description_t *)p_obj;
+                                        &p_obj->extended_content_description;
     int i;
 
     for( i = 0; i < p_ec->i_count; i++ )
@@ -1265,7 +1267,7 @@ static void ASF_FreeObject_XXX( asf_object_t *p_obj)
 
 
 /* */
-static struct
+static const struct
 {
     const guid_t  *p_id;
     int     i_type;
@@ -1513,6 +1515,9 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
     asf_object_t *p_obj;
 
+    if( !p_root )
+        return NULL;
+
     p_root->i_type = ASF_OBJECT_ROOT;
     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
     p_root->i_object_pos = stream_Tell( s );
@@ -1530,7 +1535,7 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
     {
         p_obj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
+        if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
         {
             free( p_obj );
             break;