Skip to content

Commit e08e29f

Browse files
qt: Fix potential infinite loop and missing include in mnemonic verification
- Guard against short mnemonics (< 3 words) to prevent infinite loop - Add validation in setupStep2() to check word count before proceeding - Add safety check after generateRandomPositions() to ensure positions were generated - Add missing #include <algorithm> for std::sort usage
1 parent 77c3aeb commit e08e29f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/qt/mnemonicverificationdialog.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <QSet>
2222
#include <QMessageBox>
2323

24+
#include <algorithm>
25+
2426
MnemonicVerificationDialog::MnemonicVerificationDialog(const SecureString& mnemonic, QWidget *parent) :
2527
QDialog(parent, GUIUtil::dialog_flags),
2628
ui(new Ui::MnemonicVerificationDialog),
@@ -141,7 +143,25 @@ void MnemonicVerificationDialog::setupStep2()
141143
ui->stackedWidget->setCurrentIndex(1);
142144
// Parse words for validation (needed in step 2)
143145
parseWords();
146+
147+
// Validate mnemonic has at least 3 words before proceeding
148+
const int wordCount = getWordCount();
149+
if (wordCount < 3) {
150+
QMessageBox::critical(this, tr("Invalid Mnemonic"),
151+
tr("Mnemonic phrase has fewer than 3 words (found %1). Verification cannot proceed.").arg(wordCount));
152+
reject();
153+
return;
154+
}
155+
144156
generateRandomPositions();
157+
158+
// Safety check: ensure positions were generated successfully
159+
if (m_selected_positions.size() < 3) {
160+
QMessageBox::critical(this, tr("Verification Error"),
161+
tr("Failed to generate verification positions. Please try again."));
162+
reject();
163+
return;
164+
}
145165

146166
ui->word1Edit->clear();
147167
ui->word2Edit->clear();
@@ -214,7 +234,11 @@ void MnemonicVerificationDialog::setupStep2()
214234
void MnemonicVerificationDialog::generateRandomPositions()
215235
{
216236
m_selected_positions.clear();
217-
const int n = std::max(1, getWordCount());
237+
const int n = getWordCount();
238+
if (n < 3) {
239+
// Unable to verify; bail out so the dialog can surface an error message upstream.
240+
return;
241+
}
218242
QSet<int> used;
219243
QRandomGenerator* rng = QRandomGenerator::global();
220244
while (m_selected_positions.size() < 3) {

0 commit comments

Comments
 (0)