Skip to content

Commit f7a83ba

Browse files
authored
Fix index overflow
Indexing very large CHM files caused the docNumber variable to overflow and exceed the array boundaries. Increasing the size of the docNumber and frequency variables solves this problem, but makes the old index cache incompatible. Therefore, the version number of the index cache has been increased.
1 parent d7274f1 commit f7a83ba

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lib/libebook/helper_search_index.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "helper_search_index.h"
3737

3838

39-
static const int DICT_VERSION = 4;
39+
static const int DICT_VERSION = 5;
4040

4141
namespace QtAs
4242
{
@@ -70,8 +70,8 @@ QDataStream& operator>>( QDataStream& s, Document& l )
7070

7171
QDataStream& operator<<( QDataStream& s, const Document& l )
7272
{
73-
s << ( short )l.docNumber;
74-
s << ( short )l.frequency;
73+
s << l.docNumber;
74+
s << l.frequency;
7575
return s;
7676
}
7777

@@ -347,7 +347,7 @@ void Index::writeDict( QDataStream& stream )
347347
for ( QHash<QString, Entry*>::ConstIterator it = dict.begin(); it != dict.end(); ++it )
348348
{
349349
stream << it.key();
350-
stream << ( int ) it.value()->documents.count();
350+
stream << it.value()->documents.count();
351351
stream << it.value()->documents;
352352
}
353353
}
@@ -362,7 +362,7 @@ bool Index::readDict( QDataStream& stream )
362362

363363
stream >> version;
364364

365-
if ( version < 2 )
365+
if ( version != DICT_VERSION )
366366
return false;
367367

368368
stream >> m_charssplit;
@@ -424,9 +424,9 @@ QList< QUrl > Index::query( const QStringList& terms, const QStringList& termSeq
424424

425425
for ( QVector<Document>::ConstIterator doc_it = docs.constBegin(); doc_it != docs.constEnd(); ++doc_it )
426426
{
427-
if ( ( *minDoc_it ).docNumber == ( *doc_it ).docNumber )
427+
if ( minDoc_it->docNumber == doc_it->docNumber )
428428
{
429-
( *minDoc_it ).frequency += ( *doc_it ).frequency;
429+
minDoc_it->frequency += doc_it->frequency;
430430
found = true;
431431
break;
432432
}
@@ -445,7 +445,7 @@ QList< QUrl > Index::query( const QStringList& terms, const QStringList& termSeq
445445
if ( termSeq.isEmpty() )
446446
{
447447
for ( QVector<Document>::Iterator it = minDocs.begin(); it != minDocs.end(); ++it )
448-
results << docList.at( ( int )( *it ).docNumber );
448+
results << docList.at( it->docNumber );
449449

450450
return results;
451451
}
@@ -454,7 +454,7 @@ QList< QUrl > Index::query( const QStringList& terms, const QStringList& termSeq
454454

455455
for ( QVector<Document>::Iterator it = minDocs.begin(); it != minDocs.end(); ++it )
456456
{
457-
fileName = docList[( int )( *it ).docNumber ];
457+
fileName = docList[ it->docNumber ];
458458

459459
if ( searchForPhrases( termSeq, seqWords, fileName, chmFile ) )
460460
results << fileName;

lib/libebook/helper_search_index.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ struct Document
6363
return frequency < doc.frequency;
6464
}
6565

66-
qint16 docNumber;
67-
qint16 frequency;
66+
int docNumber;
67+
int frequency;
6868
};
6969

7070
QDataStream& operator>>( QDataStream& s, Document& l );

0 commit comments

Comments
 (0)