From 4bbff581f3da6879b4509ef1046975f630656fcf Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 23 Oct 2020 15:40:16 +0100 Subject: [PATCH] Fix crash in flight-plan UI Sentry-Id: FLIGHTGEAR-ES --- src/GUI/RouteDiagram.cxx | 11 ++++++++++- src/Navaids/routePath.cxx | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/GUI/RouteDiagram.cxx b/src/GUI/RouteDiagram.cxx index 1af9fb875..e46e6716e 100644 --- a/src/GUI/RouteDiagram.cxx +++ b/src/GUI/RouteDiagram.cxx @@ -48,6 +48,8 @@ void RouteDiagram::setFlightplan(FlightPlanController *fp) } m_flightplan = fp; + m_activeLegIndex = 0; + emit flightplanChanged(fp); if (fp) { @@ -76,6 +78,11 @@ void RouteDiagram::setActiveLegIndex(int activeLegIndex) if (m_activeLegIndex == activeLegIndex) return; + if ((activeLegIndex < 0) || (activeLegIndex >= numLegs())) { + qWarning() << Q_FUNC_INFO << "invalid leg index" << activeLegIndex; + return; + } + m_activeLegIndex = activeLegIndex; emit legIndexChanged(m_activeLegIndex); @@ -132,7 +139,9 @@ void RouteDiagram::fpChanged() { FlightPlanRef fp = m_flightplan->flightplan(); m_path.reset(new RoutePath(fp)); - if (fp) { + m_activeLegIndex = 0; + + if (fp && (fp->numLegs() > 0)) { const double halfLegDistance = m_path->distanceForIndex(m_activeLegIndex) * 0.5; m_projectionCenter = m_path->positionForDistanceFrom(m_activeLegIndex, halfLegDistance); } diff --git a/src/Navaids/routePath.cxx b/src/Navaids/routePath.cxx index 82ee9961b..3fc2c0e13 100644 --- a/src/Navaids/routePath.cxx +++ b/src/Navaids/routePath.cxx @@ -1153,6 +1153,11 @@ double RoutePath::distanceForVia(Via* via, int index) const double RoutePath::trackForIndex(int index) const { + const auto sz = static_cast(d->waypoints.size()); + if ((index < 0) || (index >= sz)) { + return 0.0; + } + if (d->waypoints[index].skipped) return trackForIndex(index - 1); @@ -1165,6 +1170,11 @@ double RoutePath::trackForIndex(int index) const double RoutePath::distanceForIndex(int index) const { + const auto sz = static_cast(d->waypoints.size()); + if ((index < 0) || (index >= sz)) { + return 0.0; + } + return d->waypoints[index].pathDistanceM; }