From: Soeren Apel Date: Sun, 21 Oct 2018 16:07:25 +0000 (+0200) Subject: Fix #1035 by checking for exceptions when accessing config X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=46ebcd3f6f85092a9eb6401f6f56cee8fa08131a;p=pulseview.git Fix #1035 by checking for exceptions when accessing config Also fixes #651 because it makes Device::read_config() generic. --- diff --git a/pv/devices/device.cpp b/pv/devices/device.cpp index 855a1d82..4ce05596 100644 --- a/pv/devices/device.cpp +++ b/pv/devices/device.cpp @@ -18,11 +18,17 @@ */ #include +#include + +#include +#include +#include #include #include "device.hpp" +using std::is_same; using std::shared_ptr; using sigrok::ConfigKey; @@ -50,9 +56,7 @@ shared_ptr Device::device() const return device_; } -template -uint64_t Device::read_config(const sigrok::ConfigKey*, - const uint64_t); +template uint64_t Device::read_config(const sigrok::ConfigKey*, const uint64_t); template T Device::read_config(const ConfigKey *key, const T default_value) @@ -62,11 +66,33 @@ T Device::read_config(const ConfigKey *key, const T default_value) if (!device_) return default_value; - if (!device_->config_check(key, Capability::GET)) + if (!device_->config_check(key, Capability::GET)) { + qWarning() << QApplication::tr("Querying config key %1 is not allowed") + .arg(QString::fromStdString(key->identifier())); return default_value; - - return VariantBase::cast_dynamic>( - device_->config_get(ConfigKey::SAMPLERATE)).get(); + } + + VariantBase value; + try { + value = device_->config_get(key); + } catch (const sigrok::Error &e) { + qWarning() << QApplication::tr("Querying config key %1 resulted in %2") + .arg(QString::fromStdString(key->identifier()), e.what()); + return default_value; + } + + if (is_same::value) + return VariantBase::cast_dynamic>(value).get(); + if (is_same::value) + return VariantBase::cast_dynamic>(value).get(); + if (is_same::value) + return VariantBase::cast_dynamic>(value).get(); + if (is_same::value) + return VariantBase::cast_dynamic>(value).get(); + + qWarning() << QApplication::tr("Unknown type supplied when attempting to query %1") + .arg(QString::fromStdString(key->identifier())); + return default_value; } void Device::start() diff --git a/pv/prop/bool.cpp b/pv/prop/bool.cpp index 6c89741f..0cd16046 100644 --- a/pv/prop/bool.cpp +++ b/pv/prop/bool.cpp @@ -20,6 +20,9 @@ #include #include +#include + +#include #include "bool.hpp" @@ -40,9 +43,14 @@ QWidget* Bool::get_widget(QWidget *parent, bool auto_commit) if (!getter_) return nullptr; - Glib::VariantBase variant = getter_(); - if (!variant.gobj()) + try { + Glib::VariantBase variant = getter_(); + if (!variant.gobj()) + return nullptr; + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); return nullptr; + } check_box_ = new QCheckBox(name(), parent); check_box_->setToolTip(desc()); @@ -66,9 +74,16 @@ void Bool::update_widget() if (!check_box_) return; - Glib::VariantBase variant = getter_(); - assert(variant.gobj()); + Glib::VariantBase variant; + try { + variant = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return; + } + + assert(variant.gobj()); bool value = Glib::VariantBase::cast_dynamic>( variant).get(); @@ -82,8 +97,7 @@ void Bool::commit() if (!check_box_) return; - setter_(Glib::Variant::create( - check_box_->checkState() == Qt::Checked)); + setter_(Glib::Variant::create(check_box_->checkState() == Qt::Checked)); } void Bool::on_state_changed(int) diff --git a/pv/prop/double.cpp b/pv/prop/double.cpp index 57512d72..f39ae844 100644 --- a/pv/prop/double.cpp +++ b/pv/prop/double.cpp @@ -19,8 +19,11 @@ #include +#include #include +#include + #include "double.hpp" using boost::optional; @@ -54,9 +57,14 @@ QWidget* Double::get_widget(QWidget *parent, bool auto_commit) if (!getter_) return nullptr; - Glib::VariantBase variant = getter_(); - if (!variant.gobj()) + try { + Glib::VariantBase variant = getter_(); + if (!variant.gobj()) + return nullptr; + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); return nullptr; + } spin_box_ = new QDoubleSpinBox(parent); spin_box_->setDecimals(decimals_); @@ -80,7 +88,15 @@ void Double::update_widget() if (!spin_box_) return; - Glib::VariantBase variant = getter_(); + Glib::VariantBase variant; + + try { + variant = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return; + } + assert(variant.gobj()); double value = Glib::VariantBase::cast_dynamic>( diff --git a/pv/prop/enum.cpp b/pv/prop/enum.cpp index f8e39bf9..d62c901c 100644 --- a/pv/prop/enum.cpp +++ b/pv/prop/enum.cpp @@ -29,6 +29,8 @@ #include #include +#include + #include "enum.hpp" using std::abs; @@ -103,7 +105,15 @@ QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) if (!getter_) return nullptr; - Glib::VariantBase variant = getter_(); + Glib::VariantBase variant; + + try { + variant = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return nullptr; + } + if (!variant.gobj()) return nullptr; @@ -158,7 +168,15 @@ QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) void Enum::update_widget() { - Glib::VariantBase variant = getter_(); + Glib::VariantBase variant; + + try { + variant = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return; + } + assert(variant.gobj()); if (is_range_) { diff --git a/pv/prop/int.cpp b/pv/prop/int.cpp index ce6a040d..3f29951b 100644 --- a/pv/prop/int.cpp +++ b/pv/prop/int.cpp @@ -20,8 +20,11 @@ #include #include +#include #include +#include + #include "int.hpp" using boost::optional; @@ -56,7 +59,12 @@ QWidget* Int::get_widget(QWidget *parent, bool auto_commit) if (!getter_) return nullptr; - value_ = getter_(); + try { + value_ = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return nullptr; + } GVariant *value = value_.gobj(); if (!value) @@ -114,7 +122,13 @@ void Int::update_widget() if (!spin_box_) return; - value_ = getter_(); + try { + value_ = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return; + } + GVariant *value = value_.gobj(); assert(value); diff --git a/pv/prop/property.hpp b/pv/prop/property.hpp index ebc6412a..4ce352e1 100644 --- a/pv/prop/property.hpp +++ b/pv/prop/property.hpp @@ -63,7 +63,7 @@ protected: const Getter getter_; const Setter setter_; -private: +protected: QString name_; QString desc_; }; diff --git a/pv/prop/string.cpp b/pv/prop/string.cpp index d93d78ed..b82b496e 100644 --- a/pv/prop/string.cpp +++ b/pv/prop/string.cpp @@ -19,9 +19,12 @@ #include +#include #include #include +#include + #include "string.hpp" using std::string; @@ -48,9 +51,14 @@ QWidget* String::get_widget(QWidget *parent, bool auto_commit) if (!getter_) return nullptr; - Glib::VariantBase variant = getter_(); - if (!variant.gobj()) + try { + Glib::VariantBase variant = getter_(); + if (!variant.gobj()) + return nullptr; + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); return nullptr; + } line_edit_ = new QLineEdit(parent); @@ -68,7 +76,15 @@ void String::update_widget() if (!line_edit_) return; - Glib::VariantBase variant = getter_(); + Glib::VariantBase variant; + + try { + variant = getter_(); + } catch (const sigrok::Error &e) { + qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); + return; + } + assert(variant.gobj()); string value = Glib::VariantBase::cast_dynamic>( diff --git a/pv/toolbars/mainbar.cpp b/pv/toolbars/mainbar.cpp index 6d225369..e6beb2b3 100644 --- a/pv/toolbars/mainbar.cpp +++ b/pv/toolbars/mainbar.cpp @@ -384,7 +384,7 @@ void MainBar::update_sample_rate_selector_value() sample_rate_.set_value(samplerate); updating_sample_rate_ = false; } catch (Error& error) { - qDebug() << tr("Failed to get value of sample rate:") << error.what(); + qDebug() << tr("Failed to get sample rate:") << error.what(); } }