From 52373760e2eb9571d82b6d17151dd0a9e3e2a5ad Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Mon, 24 Jun 2019 16:39:51 +0100
Subject: [PATCH] Canvas Map: tolerate empty paths in waypoints

This fixes bogus indexing warnings and thus Nasal failures when
processing certain kinds of FlightPlan leg, such as discontinuities
and skipped legs.
---
 Nasal/canvas/map/WPT.lcontroller | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/Nasal/canvas/map/WPT.lcontroller b/Nasal/canvas/map/WPT.lcontroller
index 29585fef7..56c67f0e9 100644
--- a/Nasal/canvas/map/WPT.lcontroller
+++ b/Nasal/canvas/map/WPT.lcontroller
@@ -48,19 +48,26 @@ var WPT_model = {
 		var m = { parents:[WPT_model], idx:(idx+offset) };
 		var wp = fp.getWP(idx);
 		m.name = wp.wp_name;
-		
+
 		var alt = wp.alt_cstr;
 		if (alt != 0)
 			m.name ~= "\n"~alt;
 
-        var wp2idx = 1; # next waypoint
-
-		if (!idx) { # first point
-            if(fp.getPlanSize(idx) == 1) # if only have one wpt then it must be at element 0.
+		if (idx > 0) {
+			var path = wp.path();
+			# check for empty path
+			if (size(path) == 0)
+				return nil;
+			var n = wp.path()[-1];
+		} else {
+			var wp2idx = 1; # next waypoint
+ 			if (fp.getPlanSize() == 1) # if only have one wpt then it must be at element 0.
                 wp2idx = 0;
-        }
-		if (idx) var n = wp.path()[-1];
-		else var n = fp.getWP(wp2idx).path()[0];
+			var path = fp.getWP(wp2idx).path();
+			if (size(path) == 0)
+				return nil;
+			var n = fp.getWP(wp2idx).path()[0];
+		}
 
 		(m.lat,m.lon) = (n.lat,n.lon);
 		return m;
@@ -82,8 +89,12 @@ var searchCmd = func {
 	for(var idx = 0; idx < planCount; idx += 1){
 		var fp = driver.getFlightPlan(idx);
 		var fpSize = fp.getPlanSize(idx);
-		for (var i = 0; i < fpSize; i+=1)
-			append(result, WPT_model.new(fp, i));
+
+		for (var i = 0; i < fpSize; i+=1) {
+			var wm = WPT_model.new(fp, i);
+			if (wm != nil)
+				append(result, wm);
+		}
 	}
 	return result;
 };