From 59da0cf2b1219e6b60394d4da91f9299eb4482b9 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Fri, 1 Jan 2016 18:10:16 +0000
Subject: [PATCH] MODELDEF, DECORATE, GLDEFS parsers: fixed a crash when
 checking a file path when said path contained unsupported characters.

---
 Source/Core/GZBuilder/GZDoom/GldefsParser.cs  |  3 +++
 .../GZBuilder/GZDoom/ModeldefStructure.cs     | 18 ++++++++++------
 Source/Core/ZDoom/DecorateParser.cs           |  3 +++
 Source/Core/ZDoom/ZDTextParser.cs             | 21 +++++++++++++++++++
 4 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/Source/Core/GZBuilder/GZDoom/GldefsParser.cs b/Source/Core/GZBuilder/GZDoom/GldefsParser.cs
index 59e54b8ac..63ac4e8ed 100644
--- a/Source/Core/GZBuilder/GZDoom/GldefsParser.cs
+++ b/Source/Core/GZBuilder/GZDoom/GldefsParser.cs
@@ -660,6 +660,9 @@ namespace CodeImp.DoomBuilder.GZBuilder.GZDoom
 						return false;
 					}
 
+					// Check invalid path chars
+					if(!CheckInvalidPathChars(token)) return false;
+
 					// Absolute paths are not supported...
 					if(Path.IsPathRooted(includelump))
 					{
diff --git a/Source/Core/GZBuilder/GZDoom/ModeldefStructure.cs b/Source/Core/GZBuilder/GZDoom/ModeldefStructure.cs
index 636071034..2381f7488 100644
--- a/Source/Core/GZBuilder/GZDoom/ModeldefStructure.cs
+++ b/Source/Core/GZBuilder/GZDoom/ModeldefStructure.cs
@@ -98,15 +98,18 @@ namespace CodeImp.DoomBuilder.GZBuilder.GZDoom
 								return false;
 							} 
 
+							//check invalid path chars
+							if(!parser.CheckInvalidPathChars(token)) return false;
+
 							//check extension
-							string fileExt = Path.GetExtension(token);
-							if(string.IsNullOrEmpty(fileExt)) 
+							string modelext = Path.GetExtension(token);
+							if(string.IsNullOrEmpty(modelext)) 
 							{
 								parser.ReportError("Model '" + token + "' won't be loaded. Models without extension are not supported by GZDoom");
 								return false;
 							}
 
-							if(fileExt != ".md3" && fileExt != ".md2") 
+							if(modelext != ".md3" && modelext != ".md2") 
 							{
 								parser.ReportError("Model '" + token + "' won't be loaded. Only MD2 and MD3 models are supported");
 								return false;
@@ -149,11 +152,14 @@ namespace CodeImp.DoomBuilder.GZBuilder.GZDoom
 								return false;
 							} 
 
+							//check invalid path chars
+							if(!parser.CheckInvalidPathChars(token)) return false;
+
 							//check extension
-							string ext = Path.GetExtension(token);
-							if(Array.IndexOf(ModelData.SUPPORTED_TEXTURE_EXTENSIONS, ext) == -1) 
+							string texext = Path.GetExtension(token);
+							if(Array.IndexOf(ModelData.SUPPORTED_TEXTURE_EXTENSIONS, texext) == -1) 
 							{
-								parser.ReportError("Image format '" + ext + "' is not supported");
+								parser.ReportError("Image format '" + texext + "' is not supported");
 								return false;
 							} 
 
diff --git a/Source/Core/ZDoom/DecorateParser.cs b/Source/Core/ZDoom/DecorateParser.cs
index 47f1d4f90..96d03cc4f 100644
--- a/Source/Core/ZDoom/DecorateParser.cs
+++ b/Source/Core/ZDoom/DecorateParser.cs
@@ -168,6 +168,9 @@ namespace CodeImp.DoomBuilder.ZDoom
 								return false;
 							}
 
+							//mxd. Check invalid path chars
+							if(!CheckInvalidPathChars(filename)) return false;
+
 							//mxd. Absolute paths are not supported...
 							if(Path.IsPathRooted(filename))
 							{
diff --git a/Source/Core/ZDoom/ZDTextParser.cs b/Source/Core/ZDoom/ZDTextParser.cs
index 1257a7a22..e28f31cf7 100644
--- a/Source/Core/ZDoom/ZDTextParser.cs
+++ b/Source/Core/ZDoom/ZDTextParser.cs
@@ -594,6 +594,27 @@ namespace CodeImp.DoomBuilder.ZDoom
 			return result;
 		}
 
+		//mxd. This replicates System.IO.Path.CheckInvalidPathChars() internal function
+		public bool CheckInvalidPathChars(string path)
+		{
+			foreach(char c in path)
+			{
+				int num = c;
+				switch(num)
+				{
+					case 34: case 60: case 62: case 124:
+						ReportError("unsupported character \"" + c + "\" in path \"" + path + "\".");
+						return false;
+
+					default:
+						if(num >= 32) continue;
+						goto case 34;
+				}
+			}
+
+			return true;
+		}
+
 		//mxd. Language type
 		protected abstract string GetLanguageType();
 
-- 
GitLab