Skip to content

Commit

Permalink
fix: slob truncated file check.
Browse files Browse the repository at this point in the history
fix #1541
  • Loading branch information
xiaoyifang committed Jun 14, 2024
1 parent ac8c459 commit 1e37d76
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/dict/slob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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() ) );
}

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1e37d76

Please sign in to comment.