Commit 55d4d471 authored by Denis Charmet's avatar Denis Charmet

Add try/catch when reading elements with libmatroska.

(cherry picked from commit 59c9e8309d5b435a2d85c2c9eaae979ba56ccdd9)

Conflicts:
	modules/demux/mkv/matroska_segment.cpp
parent 94afe341
...@@ -462,16 +462,24 @@ matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlS ...@@ -462,16 +462,24 @@ matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlS
EbmlElement *p_l0, *p_l1, *p_l2; EbmlElement *p_l0, *p_l1, *p_l2;
bool b_keep_stream = false, b_keep_segment = false; bool b_keep_stream = false, b_keep_segment = false;
// verify the EBML Header /* verify the EBML Header... it shouldn't be bigger than 1kB */
p_l0 = p_estream->FindNextID(EBML_INFO(EbmlHead), UINT64_MAX); p_l0 = p_estream->FindNextID(EBML_INFO(EbmlHead), 1024);
if (p_l0 == NULL) if (p_l0 == NULL)
{ {
msg_Err( p_demux, "No EBML header found" ); msg_Err( p_demux, "No EBML header found" );
return NULL; return NULL;
} }
// verify we can read this Segment, we only support Matroska version 1 for now /* verify we can read this Segment */
p_l0->Read(*p_estream, EBML_CLASS_CONTEXT(EbmlHead), i_upper_lvl, p_l0, true); try
{
p_l0->Read(*p_estream, EBML_CLASS_CONTEXT(EbmlHead), i_upper_lvl, p_l0, true);
}
catch(...)
{
msg_Err(p_demux, "EBML Header Read failed");
return NULL;
}
EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0)); EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
if (std::string(doc_type) != "matroska" && std::string(doc_type) != "webm" ) if (std::string(doc_type) != "matroska" && std::string(doc_type) != "webm" )
...@@ -517,8 +525,15 @@ matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlS ...@@ -517,8 +525,15 @@ matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlS
// find the families of this segment // find the families of this segment
KaxInfo *p_info = static_cast<KaxInfo*>(p_l1); KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
b_keep_segment = b_initial; b_keep_segment = b_initial;
try
p_info->Read(*p_estream, EBML_CLASS_CONTEXT(KaxInfo), i_upper_lvl, p_l2, true); {
p_info->Read(*p_estream, EBML_CLASS_CONTEXT(KaxInfo), i_upper_lvl, p_l2, true);
}
catch (...)
{
msg_Err( p_demux, "KaxInfo found but corrupted");
break;
}
for( size_t i = 0; i < p_info->ListSize(); i++ ) for( size_t i = 0; i < p_info->ListSize(); i++ )
{ {
EbmlElement *l = (*p_info)[i]; EbmlElement *l = (*p_info)[i];
......
This diff is collapsed.
...@@ -66,26 +66,33 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead ) ...@@ -66,26 +66,33 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
msg_Dbg( &sys.demuxer, "| | + Seek" ); msg_Dbg( &sys.demuxer, "| | + Seek" );
#endif #endif
ep->Down(); ep->Down();
while( ( l = ep->Get() ) != NULL ) try
{ {
if( MKV_IS_ID( l, KaxSeekID ) ) while( ( l = ep->Get() ) != NULL )
{ {
KaxSeekID &sid = *(KaxSeekID*)l; if( MKV_IS_ID( l, KaxSeekID ) )
sid.ReadData( es.I_O() ); {
id = EbmlId( sid.GetBuffer(), sid.GetSize() ); KaxSeekID &sid = *(KaxSeekID*)l;
} sid.ReadData( es.I_O() );
else if( MKV_IS_ID( l, KaxSeekPosition ) ) id = EbmlId( sid.GetBuffer(), sid.GetSize() );
{ }
KaxSeekPosition &spos = *(KaxSeekPosition*)l; else if( MKV_IS_ID( l, KaxSeekPosition ) )
spos.ReadData( es.I_O() ); {
i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) ); KaxSeekPosition &spos = *(KaxSeekPosition*)l;
} spos.ReadData( es.I_O() );
else i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
{ }
/* Many mkvmerge files hit this case. It seems to be a broken SeekHead */ else
msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() ); {
/* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
}
} }
} }
catch(...)
{
msg_Err( &sys.demuxer,"Error while reading %s", typeid(*l).name() );
}
ep->Up(); ep->Up();
if( i_pos >= 0 ) if( i_pos >= 0 )
...@@ -668,7 +675,15 @@ void matroska_segment_c::ParseTracks( KaxTracks *tracks ) ...@@ -668,7 +675,15 @@ void matroska_segment_c::ParseTracks( KaxTracks *tracks )
int i_upper_level = 0; int i_upper_level = 0;
/* Master elements */ /* Master elements */
tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true ); try
{
tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
}
catch(...)
{
msg_Err( &sys.demuxer, "Couldn't read tracks" );
return;
}
for( size_t i = 0; i < tracks->ListSize(); i++ ) for( size_t i = 0; i < tracks->ListSize(); i++ )
{ {
...@@ -696,7 +711,15 @@ void matroska_segment_c::ParseInfo( KaxInfo *info ) ...@@ -696,7 +711,15 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
/* Master elements */ /* Master elements */
m = static_cast<EbmlMaster *>(info); m = static_cast<EbmlMaster *>(info);
m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true ); try
{
m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
}
catch(...)
{
msg_Err( &sys.demuxer, "Couldn't read info" );
return;
}
for( size_t i = 0; i < m->ListSize(); i++ ) for( size_t i = 0; i < m->ListSize(); i++ )
{ {
...@@ -809,28 +832,35 @@ void matroska_segment_c::ParseInfo( KaxInfo *info ) ...@@ -809,28 +832,35 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
else if( MKV_IS_ID( l, KaxChapterTranslate ) ) else if( MKV_IS_ID( l, KaxChapterTranslate ) )
{ {
KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l ); KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
chapter_translation_c *p_translate = new chapter_translation_c(); try
p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
for( size_t j = 0; j < p_trans->ListSize(); j++ )
{ {
EbmlElement *l = (*p_trans)[j]; p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
chapter_translation_c *p_translate = new chapter_translation_c();
if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) ) for( size_t j = 0; j < p_trans->ListSize(); j++ )
{
p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
}
else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
{
p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
}
else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
{ {
p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ); EbmlElement *l = (*p_trans)[j];
if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
{
p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
}
else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
{
p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
}
else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
{
p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
}
} }
}
translations.push_back( p_translate ); translations.push_back( p_translate );
}
catch(...)
{
msg_Err( &sys.demuxer, "Error while reading Chapter Tranlate");
}
} }
else else
{ {
...@@ -994,7 +1024,15 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments ) ...@@ -994,7 +1024,15 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
EbmlElement *el; EbmlElement *el;
int i_upper_level = 0; int i_upper_level = 0;
attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true ); try
{
attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
}
catch(...)
{
msg_Err( &sys.demuxer, "Error while reading attachments" );
return;
}
KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments ); KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
...@@ -1041,7 +1079,15 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters ) ...@@ -1041,7 +1079,15 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
int i_upper_level = 0; int i_upper_level = 0;
/* Master elements */ /* Master elements */
chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true ); try
{
chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
}
catch(...)
{
msg_Err( &sys.demuxer, "Error while reading chapters" );
return;
}
for( size_t i = 0; i < chapters->ListSize(); i++ ) for( size_t i = 0; i < chapters->ListSize(); i++ )
{ {
...@@ -1102,7 +1148,15 @@ void matroska_segment_c::ParseCluster( bool b_update_start_time ) ...@@ -1102,7 +1148,15 @@ void matroska_segment_c::ParseCluster( bool b_update_start_time )
/* Master elements */ /* Master elements */
m = static_cast<EbmlMaster *>( cluster ); m = static_cast<EbmlMaster *>( cluster );
m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true ); try
{
m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
}
catch(...)
{
msg_Err( &sys.demuxer, "Error while reading cluster" );
return;
}
for( unsigned int i = 0; i < m->ListSize(); i++ ) for( unsigned int i = 0; i < m->ListSize(); i++ )
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment