]> sigrok.org Git - pulseview.git/commitdiff
Fix #1035 by checking for exceptions when accessing config
authorSoeren Apel <redacted>
Sun, 21 Oct 2018 16:07:25 +0000 (18:07 +0200)
committerSoeren Apel <redacted>
Sun, 21 Oct 2018 16:12:22 +0000 (18:12 +0200)
Also fixes #651 because it makes Device::read_config()
generic.

pv/devices/device.cpp
pv/prop/bool.cpp
pv/prop/double.cpp
pv/prop/enum.cpp
pv/prop/int.cpp
pv/prop/property.hpp
pv/prop/string.cpp
pv/toolbars/mainbar.cpp

index 855a1d82fca65019952b1baf46f21aef60c859de..4ce055965167bc842d766269ff259f8cc45454fa 100644 (file)
  */
 
 #include <cassert>
+#include <type_traits>
+
+#include <QApplication>
+#include <QDebug>
+#include <QString>
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
 #include "device.hpp"
 
+using std::is_same;
 using std::shared_ptr;
 
 using sigrok::ConfigKey;
@@ -50,9 +56,7 @@ shared_ptr<sigrok::Device> 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<typename T>
 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<Glib::Variant<guint64>>(
-               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<T, uint32_t>::value)
+               return VariantBase::cast_dynamic<Glib::Variant<guint32>>(value).get();
+       if (is_same<T, int32_t>::value)
+               return VariantBase::cast_dynamic<Glib::Variant<gint32>>(value).get();
+       if (is_same<T, uint64_t>::value)
+               return VariantBase::cast_dynamic<Glib::Variant<guint64>>(value).get();
+       if (is_same<T, int64_t>::value)
+               return VariantBase::cast_dynamic<Glib::Variant<gint64>>(value).get();
+
+       qWarning() << QApplication::tr("Unknown type supplied when attempting to query %1")
+               .arg(QString::fromStdString(key->identifier()));
+       return default_value;
 }
 
 void Device::start()
index 6c89741ff6f6657779d74f4a6e30d0c7675d72d4..0cd1604612661730348acd099580f7ac02947725 100644 (file)
@@ -20,6 +20,9 @@
 #include <cassert>
 
 #include <QCheckBox>
+#include <QDebug>
+
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
 #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<Glib::Variant<bool>>(
                variant).get();
 
@@ -82,8 +97,7 @@ void Bool::commit()
        if (!check_box_)
                return;
 
-       setter_(Glib::Variant<bool>::create(
-               check_box_->checkState() == Qt::Checked));
+       setter_(Glib::Variant<bool>::create(check_box_->checkState() == Qt::Checked));
 }
 
 void Bool::on_state_changed(int)
index 57512d723ff26b5cd3473432040e975cf914e8bb..f39ae844fc97ce34b4519d4a67546a867e8cdb74 100644 (file)
 
 #include <cassert>
 
+#include <QDebug>
 #include <QDoubleSpinBox>
 
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
 #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<Glib::Variant<double>>(
index f8e39bf95cd323d1f8303df113e6706537af3999..d62c901cf4c70d2c3848b3a3875d9d088596b794 100644 (file)
@@ -29,6 +29,8 @@
 #include <QLabel>
 #include <QSlider>
 
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
 #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_) {
index ce6a040d633c00c1a2583ba6fe9fc85f443f136c..3f29951b3d997b7b5b8a299fd34c3a0bf0d6db40 100644 (file)
 #include <cassert>
 #include <cstdint>
 
+#include <QDebug>
 #include <QSpinBox>
 
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
 #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);
 
index ebc6412a0bf0f2746be61b00b63ff30289a34b34..4ce352e11e5dacb2aaacfe4e726e3e94875daaef 100644 (file)
@@ -63,7 +63,7 @@ protected:
        const Getter getter_;
        const Setter setter_;
 
-private:
+protected:
        QString name_;
        QString desc_;
 };
index d93d78eddf72589190cee89a2f0a082c76a9204b..b82b496eed42175f7361cd792a4ab8196036eee7 100644 (file)
 
 #include <cassert>
 
+#include <QDebug>
 #include <QLineEdit>
 #include <QSpinBox>
 
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
 #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<Glib::Variant<ustring>>(
index 6d225369dda8516563160dec278167b63c6c3c9f..e6beb2b3ba704e198c8aff0076787a9d96798fe9 100644 (file)
@@ -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();
        }
 }