From 04d9dc43f67b102c7727228326c05eb82d87e4f2 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Thu, 17 Jan 2019 17:53:13 +0300 Subject: [PATCH] Fix some crashes (issue #1076) --- bgl.cc | 9 ++++++--- dictdfiles.cc | 7 +++++-- dsl.cc | 14 ++++++++++---- epwing.cc | 7 +++++-- gls.cc | 7 +++++-- mdx.cc | 16 +++++++++++----- sdict.cc | 7 +++++-- stardict.cc | 3 +++ utf8.cc | 3 +++ 9 files changed, 53 insertions(+), 20 deletions(-) diff --git a/bgl.cc b/bgl.cc index decda949..09921bd8 100644 --- a/bgl.cc +++ b/bgl.cc @@ -289,11 +289,14 @@ namespace size_t len = idx.read< uint32_t >(); - vector< char > nameBuf( len ); + if( len ) + { + vector< char > nameBuf( len ); - idx.read( &nameBuf.front(), len ); + idx.read( &nameBuf.front(), len ); - dictionaryName = string( &nameBuf.front(), len ); + dictionaryName = string( &nameBuf.front(), len ); + } // Initialize the index diff --git a/dictdfiles.cc b/dictdfiles.cc index d503fc94..d633c23d 100644 --- a/dictdfiles.cc +++ b/dictdfiles.cc @@ -161,8 +161,11 @@ DictdDictionary::DictdDictionary( string const & id, idx.seek( sizeof( idxHeader ) ); vector< char > dName( idx.read< uint32_t >() ); - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); + if( dName.size() > 0 ) + { + idx.read( &dName.front(), dName.size() ); + dictionaryName = string( &dName.front(), dName.size() ); + } // Open the .dict file diff --git a/dsl.cc b/dsl.cc index aa1d1ac8..f9b1f9c8 100644 --- a/dsl.cc +++ b/dsl.cc @@ -310,12 +310,18 @@ DslDictionary::DslDictionary( string const & id, idx.seek( sizeof( idxHeader ) ); vector< char > dName( idx.read< uint32_t >() ); - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); + if( dName.size() > 0 ) + { + idx.read( &dName.front(), dName.size() ); + dictionaryName = string( &dName.front(), dName.size() ); + } vector< char > sName( idx.read< uint32_t >() ); - idx.read( &sName.front(), sName.size() ); - preferredSoundDictionary = string( &sName.front(), sName.size() ); + if( sName.size() > 0 ) + { + idx.read( &sName.front(), sName.size() ); + preferredSoundDictionary = string( &sName.front(), sName.size() ); + } // Everything else would be done in deferred init } diff --git a/epwing.cc b/epwing.cc index b3468948..76866cb9 100644 --- a/epwing.cc +++ b/epwing.cc @@ -204,8 +204,11 @@ EpwingDictionary::EpwingDictionary( string const & id, { vector< char > data( idxHeader.nameSize ); idx.seek( sizeof( idxHeader ) ); - idx.read( &data.front(), idxHeader.nameSize ); - bookName = string( &data.front(), idxHeader.nameSize ); + if( data.size() > 0 ) + { + idx.read( &data.front(), idxHeader.nameSize ); + bookName = string( &data.front(), idxHeader.nameSize ); + } // Initialize eBook diff --git a/gls.cc b/gls.cc index b76a6de3..be067788 100644 --- a/gls.cc +++ b/gls.cc @@ -549,8 +549,11 @@ GlsDictionary::GlsDictionary( string const & id, idx.seek( sizeof( idxHeader ) ); vector< char > dName( idx.read< uint32_t >() ); - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); + if( dName.size() > 0 ) + { + idx.read( &dName.front(), dName.size() ); + dictionaryName = string( &dName.front(), dName.size() ); + } // Initialize the index diff --git a/mdx.cc b/mdx.cc index 9f6e94f1..b954374c 100644 --- a/mdx.cc +++ b/mdx.cc @@ -303,14 +303,20 @@ MdxDictionary::MdxDictionary( string const & id, string const & indexFile, idx.seek( sizeof( idxHeader ) ); size_t len = idx.read< uint32_t >(); vector< char > buf( len ); - idx.read( &buf.front(), len ); - dictionaryName = string( &buf.front(), len ); + if( len > 0 ) + { + idx.read( &buf.front(), len ); + dictionaryName = string( &buf.front(), len ); + } // then read the dictionary's encoding len = idx.read< uint32_t >(); - buf.resize( len ); - idx.read( &buf.front(), len ); - encoding = string( &buf.front(), len ); + if( len > 0 ) + { + buf.resize( len ); + idx.read( &buf.front(), len ); + encoding = string( &buf.front(), len ); + } dictFile.setFileName( QString::fromUtf8( dictionaryFiles[ 0 ].c_str() ) ); dictFile.open( QIODevice::ReadOnly ); diff --git a/sdict.cc b/sdict.cc index 312a522d..4bbda718 100644 --- a/sdict.cc +++ b/sdict.cc @@ -212,8 +212,11 @@ SdictDictionary::SdictDictionary( string const & id, idx.seek( sizeof( idxHeader ) ); vector< char > dName( idx.read< uint32_t >() ); - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); + if( dName.size() > 0 ) + { + idx.read( &dName.front(), dName.size() ); + dictionaryName = string( &dName.front(), dName.size() ); + } // Initialize the index diff --git a/stardict.cc b/stardict.cc index 18632375..9363d7dc 100644 --- a/stardict.cc +++ b/stardict.cc @@ -312,6 +312,9 @@ void StardictDictionary::loadIcon() throw() string StardictDictionary::loadString( size_t size ) { + if( size == 0 ) + return string(); + vector< char > data( size ); idx.read( &data.front(), data.size() ); diff --git a/utf8.cc b/utf8.cc index f8ea8f46..c7e516f4 100644 --- a/utf8.cc +++ b/utf8.cc @@ -133,6 +133,9 @@ long decode( char const * in_, size_t inSize, wchar * out_ ) string encode( wstring const & in ) throw() { + if( in.size() == 0 ) + return string(); + std::vector< char > buffer( in.size() * 4 ); return string( &buffer.front(),