From e3cd0891d312c448db6d6a19464efd3c521cd153 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 6 Sep 2019 22:01:59 +0200 Subject: [PATCH] Dive site: close dive site edit widget when dive site is deleted The application could be crashed by 1) Create dive site 2) Edit dive site 3) Undo until dive site is removed 4) Continue editing now non-existing dive site Therefore, hook into the dive-site-deleted signal and if the currently edited dive site is deleted, close the widget. When closing the widget, make sure that the potentially dangling pointer is reset to zero so that there is no other potential use-after-free bug. Signed-off-by: Berthold Stoeger --- CHANGELOG.md | 1 + desktop-widgets/locationinformation.cpp | 12 ++++++++++++ desktop-widgets/locationinformation.h | 1 + 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4add1764e..def8ec6ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +- Desktop: close dive site widget if dive site is removed by undo - Desktop: Don't destroy format of planner notes when editing dive notes [#2265] - Map: avoid full map reload when clicking on flag - Map: highlight all selected dive sites when clicking on flag diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index a210ddf44..37bced819 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -36,6 +36,7 @@ LocationInformationWidget::LocationInformationWidget(QWidget *parent) : QGroupBo ui.diveSiteCoordinates->installEventFilter(this); connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &LocationInformationWidget::diveSiteChanged); + connect(&diveListNotifier, &DiveListNotifier::diveSiteDeleted, this, &LocationInformationWidget::diveSiteDeleted); connect(qPrefUnits::instance(), &qPrefUnits::unit_systemChanged, this, &LocationInformationWidget::unitsChanged); unitsChanged(); @@ -178,8 +179,19 @@ static location_t parseGpsText(const QString &text) return { {0}, {0} }; } +void LocationInformationWidget::diveSiteDeleted(struct dive_site *ds, int) +{ + // If the currently edited dive site was removed under our feet, close the widget. + // This will reset the dangling pointer. + if (ds && ds == diveSite) + acceptChanges(); +} + void LocationInformationWidget::acceptChanges() { + diveSite = nullptr; + closeDistance = 0; + MainWindow::instance()->diveList->setEnabled(true); MainWindow::instance()->setEnabledToolbar(true); MainWindow::instance()->setApplicationState(ApplicationState::Default); diff --git a/desktop-widgets/locationinformation.h b/desktop-widgets/locationinformation.h index b6122d2fe..0e53a4439 100644 --- a/desktop-widgets/locationinformation.h +++ b/desktop-widgets/locationinformation.h @@ -34,6 +34,7 @@ public slots: private slots: void updateLabels(); void diveSiteChanged(struct dive_site *ds, int field); + void diveSiteDeleted(struct dive_site *ds, int); void unitsChanged(); private: void keyPressEvent(QKeyEvent *e) override;