From 1e37d760e2acecf9cb134a93e32fd1135e77efaf Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Fri, 14 Jun 2024 22:30:16 +0800 Subject: [PATCH 1/2] fix: slob truncated file check. fix #1541 --- src/dict/slob.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/dict/slob.cc b/src/dict/slob.cc index 8029a825..c0a6ebe8 100644 --- a/src/dict/slob.cc +++ b/src/dict/slob.cc @@ -58,6 +58,7 @@ using BtreeIndexing::IndexedWords; using BtreeIndexing::IndexInfo; DEF_EX_STR( exNotSlobFile, "Not an Slob file", Dictionary::Ex ) +DEF_EX( exTruncateFile, "Slob file truncated", Dictionary::Ex ) using Dictionary::exCantReadFile; DEF_EX_STR( exCantDecodeFile, "Can't decode file", Dictionary::Ex ) DEF_EX_STR( exNoCodecFound, "No text codec found", Dictionary::Ex ) @@ -323,8 +324,8 @@ void SlobFile::open( const QString & name ) encoding = readTinyText(); codec = QTextCodec::codecForName( encoding.toLatin1() ); - if ( codec == 0 ) { - error = QString( "for encoding \"" ) + encoding + "\""; + if ( codec == nullptr ) { + error = QString( R"(for encoding "%1")" ).arg( encoding ); throw exNoCodecFound( string( error.toUtf8().data() ) ); } @@ -383,6 +384,11 @@ void SlobFile::open( const QString & name ) break; fileSize = qFromBigEndian( tmp ); + //truncated file + if ( file.size() < fileSize ) { + throw exTruncateFile(); + } + if ( file.read( (char *)&cnt, sizeof( cnt ) ) != sizeof( cnt ) ) break; refsCount = qFromBigEndian( cnt ); @@ -678,6 +684,7 @@ SlobDictionary::SlobDictionary( string const & id, string const & indexFile, vec } catch ( std::exception & e ) { gdWarning( "Slob dictionary initializing failed: %s, error: %s\n", dictionaryFiles[ 0 ].c_str(), e.what() ); + return; } // Initialize the indexes From 5712f63b8ef2243ddc98245e923fcb7123552c4d Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Fri, 14 Jun 2024 22:52:15 +0800 Subject: [PATCH 2/2] fix: slob truncated file check. fix #1541 --- src/dict/slob.cc | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/dict/slob.cc b/src/dict/slob.cc index c0a6ebe8..3b73508b 100644 --- a/src/dict/slob.cc +++ b/src/dict/slob.cc @@ -678,14 +678,7 @@ SlobDictionary::SlobDictionary( string const & id, string const & indexFile, vec idxHeader( idx.read< IdxHeader >() ) { // Open data file - - try { - sf.open( dictionaryFiles[ 0 ].c_str() ); - } - catch ( std::exception & e ) { - gdWarning( "Slob dictionary initializing failed: %s, error: %s\n", dictionaryFiles[ 0 ].c_str(), e.what() ); - return; - } + sf.open( dictionaryFiles[ 0 ].c_str() ); // Initialize the indexes