From d69935679cb4b2057274140b3194597ac2ce1582 Mon Sep 17 00:00:00 2001 From: Aaron Rainbolt Date: Mon, 27 Nov 2023 19:03:29 -0600 Subject: [PATCH] Add extra IPv6 address validation, fix crash when saving IPv6 settings --- ipv6settingstab.cpp | 38 ++++++++++++++++++++++++++++++++++++++ ipv6settingstab.h | 3 +++ main.cpp | 4 ++++ 3 files changed, 45 insertions(+) diff --git a/ipv6settingstab.cpp b/ipv6settingstab.cpp index c3bae8f..9e4ce1b 100644 --- a/ipv6settingstab.cpp +++ b/ipv6settingstab.cpp @@ -22,6 +22,11 @@ Ipv6SettingsTab::~Ipv6SettingsTab() QVariantMap Ipv6SettingsTab::readSettings() { + // If the user has invalid data in here, just return the original settings data + if (!checkValidity()) { + return origSettings; + } + QVariantMap output; switch (ui->methodComboBox->currentIndex()) { case 0: // Automatic @@ -79,6 +84,7 @@ QVariantMap Ipv6SettingsTab::readSettings() void Ipv6SettingsTab::loadSettings(QVariantMap settings) { + origSettings = settings; // We may need to restore these if something about the settings the user puts in is invalid. if (settings["ipv6Method"].isValid()) { switch (settings["ipv6Method"].toInt()) { case ConnectionSettingsEngine::Ipv6Automatic: @@ -179,3 +185,35 @@ void Ipv6SettingsTab::onManualIpv6ConfigurationRemoveButtonClicked() { ui->manualIpv6ConfigurationTable->removeRow(ui->manualIpv6ConfigurationTable->currentRow()); } + +bool Ipv6SettingsTab::checkValidity() +{ + // almost copied verbatim from plasma-nm + if (ui->methodComboBox->currentIndex() == 4) { // manual + if (!ui->manualIpv6ConfigurationTable->rowCount()) { + return false; + } + + for (int i = 0, rowCount = ui->manualIpv6ConfigurationTable->rowCount(); i < rowCount; i++) { + QHostAddress ip = QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 0)->text()); + const int prefix = ui->manualIpv6ConfigurationTable->item(i, 1)->text().toInt(); + QHostAddress gateway = QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 2)->text()); + + if (ip.isNull() || !(prefix >= 1 && prefix <= 128) || (gateway.isNull() && !ui->manualIpv6ConfigurationTable->item(i, 2)->text().isEmpty())) { + return false; + } + } + } + + if (!ui->dnsServersLineEdit->text().isEmpty() && (ui->methodComboBox->currentIndex() == 0 || ui->methodComboBox->currentIndex() == 1 || ui->methodComboBox->currentIndex() == 4)) { // Automatic, AutomaticIPOnly, or Manual + const QStringList tmp = ui->dnsServersLineEdit->text().split(QLatin1Char(',')); + for (const QString &str : tmp) { + QHostAddress addr(str); + if (addr.isNull()) { + return false; + } + } + } + + return true; +} diff --git a/ipv6settingstab.h b/ipv6settingstab.h index 2514bce..a076144 100644 --- a/ipv6settingstab.h +++ b/ipv6settingstab.h @@ -30,6 +30,9 @@ private slots: private: Ui::Ipv6SettingsTab *ui; + QVariantMap origSettings; + + bool checkValidity(); }; #endif // IPV6SETTINGSTAB_H diff --git a/main.cpp b/main.cpp index 1dd1721..2c11c04 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include int main(int argc, char *argv[]) { @@ -18,6 +19,9 @@ int main(int argc, char *argv[]) break; } } + + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); NetworkSelector w; w.show(); return a.exec();