From 781a3baceb8f3ce9785bf5f2ed61a8f24be3c7c2 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Sat, 3 Apr 2021 17:50:43 +0100 Subject: [PATCH 1/2] fix(regionparser): handle invalid number of region points fix #319 --- src/Compiler/Parser/RegionParser.cs | 8 ++++++++ tests/CompilerTest/Parser/RegionParserTest.cs | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Compiler/Parser/RegionParser.cs b/src/Compiler/Parser/RegionParser.cs index 39ad5284..2a38ce68 100644 --- a/src/Compiler/Parser/RegionParser.cs +++ b/src/Compiler/Parser/RegionParser.cs @@ -126,6 +126,14 @@ public void ParseData(AbstractSectorDataFile data) continue; } + if (line.dataSegments.Count != 2) + { + this.eventLogger.AddEvent( + new SyntaxError("Invalid number of points in REGION" + data.CurrentLine, line) + ); + return; + } + Point parsedPoint = PointParser.Parse(line.dataSegments[0], line.dataSegments[1]); if (Equals(parsedPoint, PointParser.InvalidPoint)) { diff --git a/tests/CompilerTest/Parser/RegionParserTest.cs b/tests/CompilerTest/Parser/RegionParserTest.cs index 411ac737..3e0cc4ed 100644 --- a/tests/CompilerTest/Parser/RegionParserTest.cs +++ b/tests/CompilerTest/Parser/RegionParserTest.cs @@ -156,6 +156,24 @@ public void TestItAddsMultipleRegionsData() "White JSY JSY" } }, // Incomplete mid-file + new object[] + { + new List + { + "REGIONNAME TestRegion", + "Red BCN BCN ;comment", + "A" + } + }, // Not enough points data + new object[] + { + new List + { + "REGIONNAME TestRegion", + "Red BCN BCN ;comment", + "A B C" + } + } // Too much points data }; [Theory] From a724642deed79a68f264c4bd7c583d88ade543e7 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Sun, 4 Apr 2021 15:23:29 +0100 Subject: [PATCH 2/2] Make 2 not magic --- src/Compiler/Parser/RegionParser.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Parser/RegionParser.cs b/src/Compiler/Parser/RegionParser.cs index 2a38ce68..b2582cb5 100644 --- a/src/Compiler/Parser/RegionParser.cs +++ b/src/Compiler/Parser/RegionParser.cs @@ -53,6 +53,7 @@ public void ParseData(AbstractSectorDataFile data) // Expecting a colour definition for the region if (expectingColourDefinition) { + // On lines where we're expecting a colour, we want to see a colour followed by point/latlong if (line.dataSegments.Count != 3) { this.eventLogger.AddEvent( @@ -125,7 +126,8 @@ public void ParseData(AbstractSectorDataFile data) expectingColourDefinition = true; continue; } - + + // On lines without colours, we expect to see a point/latlong only if (line.dataSegments.Count != 2) { this.eventLogger.AddEvent(