From 173e600cf23b57c56b0c1c979e96028c0878a80e Mon Sep 17 00:00:00 2001 From: MaxXor Date: Mon, 3 Aug 2015 22:26:12 +0200 Subject: [PATCH] Improved error handling when parsing hosts list --- Client/Core/Helper/HostHelper.cs | 11 ++++++----- Server/Core/Helper/HostHelper.cs | 13 +++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Client/Core/Helper/HostHelper.cs b/Client/Core/Helper/HostHelper.cs index 5bd4db75..c74791ea 100644 --- a/Client/Core/Helper/HostHelper.cs +++ b/Client/Core/Helper/HostHelper.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; using xClient.Core.Utilities; @@ -12,14 +11,16 @@ public static List GetHostsList(string rawHosts) { List hostsList = new List(); + if (string.IsNullOrEmpty(rawHosts)) return hostsList; + var hosts = rawHosts.Split(';'); - foreach (var hostPart in from host in hosts where !string.IsNullOrEmpty(host) select host.Split(':')) + foreach (var hostPart in from host in hosts where (!string.IsNullOrEmpty(host) && host.Contains(':')) select host.Split(':')) { - if (hostPart.Length != 2 || hostPart[0].Length < 1 || hostPart[1].Length < 1) throw new Exception("Invalid host"); + if (hostPart.Length != 2 || hostPart[0].Length < 1 || hostPart[1].Length < 1) continue; // invalid, ignore host ushort port; - if (!ushort.TryParse(hostPart[1], out port)) throw new Exception("Invalid host"); + if (!ushort.TryParse(hostPart[1], out port)) continue; // invalid, ignore host hostsList.Add(new Host { Hostname = hostPart[0], Port = port }); } diff --git a/Server/Core/Helper/HostHelper.cs b/Server/Core/Helper/HostHelper.cs index a04f63a0..6e306b7b 100644 --- a/Server/Core/Helper/HostHelper.cs +++ b/Server/Core/Helper/HostHelper.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; using xServer.Core.Utilities; @@ -12,16 +11,18 @@ public static List GetHostsList(string rawHosts) { List hostsList = new List(); + if (string.IsNullOrEmpty(rawHosts)) return hostsList; + var hosts = rawHosts.Split(';'); - foreach (var hostPart in from host in hosts where !string.IsNullOrEmpty(host) select host.Split(':')) + foreach (var hostPart in from host in hosts where (!string.IsNullOrEmpty(host) && host.Contains(':')) select host.Split(':')) { - if (hostPart.Length != 2 || hostPart[0].Length < 1 || hostPart[1].Length < 1) throw new Exception("Invalid host"); + if (hostPart.Length != 2 || hostPart[0].Length < 1 || hostPart[1].Length < 1) continue; // invalid, ignore host ushort port; - if (!ushort.TryParse(hostPart[1], out port)) throw new Exception("Invalid host"); + if (!ushort.TryParse(hostPart[1], out port)) continue; // invalid, ignore host - hostsList.Add(new Host {Hostname = hostPart[0], Port = port}); + hostsList.Add(new Host { Hostname = hostPart[0], Port = port }); } return hostsList;