Fix regression introduced in 6723f42

Use of QString for static DefaultSeparator lead to crashes on Windows
This commit is contained in:
Janek Bevendorff 2018-02-11 15:08:07 +01:00
parent 13dc1e02ab
commit ab3775d4c5
3 changed files with 20 additions and 26 deletions

View File

@ -93,7 +93,7 @@ QString FilePath::pluginPath(const QString& name)
QString FilePath::wordlistPath(const QString& name) QString FilePath::wordlistPath(const QString& name)
{ {
return m_instance->dataPath("wordlists/" + name); return dataPath("wordlists/" + name);
} }
QIcon FilePath::applicationIcon() QIcon FilePath::applicationIcon()

View File

@ -17,32 +17,31 @@
#include "PassphraseGenerator.h" #include "PassphraseGenerator.h"
#include <math.h> #include <cmath>
#include <QFile> #include <QFile>
#include <QTextStream> #include <QTextStream>
#include "crypto/Random.h" #include "crypto/Random.h"
#include "core/FilePath.h" #include "core/FilePath.h"
const QString PassphraseGenerator::DefaultSeparator = " "; const char* PassphraseGenerator::DefaultSeparator = " ";
const QString PassphraseGenerator::DefaultWordList = "eff_large.wordlist"; const char* PassphraseGenerator::DefaultWordList = "eff_large.wordlist";
PassphraseGenerator::PassphraseGenerator() PassphraseGenerator::PassphraseGenerator()
: m_wordCount(0) : m_wordCount(0)
, m_separator(PassphraseGenerator::DefaultSeparator) , m_separator(PassphraseGenerator::DefaultSeparator)
{ {
} }
double PassphraseGenerator::calculateEntropy(QString passphrase) double PassphraseGenerator::calculateEntropy(const QString& passphrase)
{ {
Q_UNUSED(passphrase); Q_UNUSED(passphrase);
if (m_wordlist.size() == 0) { if (m_wordlist.isEmpty()) {
return 0; return 0.0;
} }
return log(m_wordlist.size()) / log(2.0) * m_wordCount; return std::log2(m_wordlist.size()) * m_wordCount;
} }
void PassphraseGenerator::setWordCount(int wordCount) void PassphraseGenerator::setWordCount(int wordCount)
@ -56,7 +55,7 @@ void PassphraseGenerator::setWordCount(int wordCount)
} }
void PassphraseGenerator::setWordList(QString path) void PassphraseGenerator::setWordList(const QString& path)
{ {
m_wordlist.clear(); m_wordlist.clear();
@ -83,7 +82,7 @@ void PassphraseGenerator::setDefaultWordList()
setWordList(path); setWordList(path);
} }
void PassphraseGenerator::setWordSeparator(QString separator) { void PassphraseGenerator::setWordSeparator(const QString& separator) {
m_separator = separator; m_separator = separator;
} }
@ -97,8 +96,8 @@ QString PassphraseGenerator::generatePassphrase() const
} }
QStringList words; QStringList words;
for (int i = 0; i < m_wordCount; i++) { for (int i = 0; i < m_wordCount; ++i) {
int wordIndex = randomGen()->randomUInt(m_wordlist.length()); int wordIndex = randomGen()->randomUInt(static_cast<quint32>(m_wordlist.length()));
words.append(m_wordlist.at(wordIndex)); words.append(m_wordlist.at(wordIndex));
} }
@ -111,9 +110,5 @@ bool PassphraseGenerator::isValid() const
return false; return false;
} }
if (m_wordlist.size() < 1000) { return m_wordlist.size() >= 1000;
return false;
}
return true;
} }

View File

@ -26,26 +26,25 @@ class PassphraseGenerator
{ {
public: public:
PassphraseGenerator(); PassphraseGenerator();
Q_DISABLE_COPY(PassphraseGenerator)
double calculateEntropy(QString passphrase); double calculateEntropy(const QString& passphrase);
void setWordCount(int wordCount); void setWordCount(int wordCount);
void setWordList(QString path); void setWordList(const QString& path);
void setDefaultWordList(); void setDefaultWordList();
void setWordSeparator(QString separator); void setWordSeparator(const QString& separator);
bool isValid() const; bool isValid() const;
QString generatePassphrase() const; QString generatePassphrase() const;
static const int DefaultWordCount = 7; static constexpr int DefaultWordCount = 7;
static const QString DefaultSeparator; static const char* DefaultSeparator;
static const QString DefaultWordList; static const char* DefaultWordList;
private: private:
int m_wordCount; int m_wordCount;
QString m_separator; QString m_separator;
QVector<QString> m_wordlist; QVector<QString> m_wordlist;
Q_DISABLE_COPY(PassphraseGenerator)
}; };
#endif // KEEPASSX_PASSPHRASEGENERATOR_H #endif // KEEPASSX_PASSPHRASEGENERATOR_H