From f33b4975f39a2cf187c2a94149ea0508f43e717a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 20 Sep 2016 22:01:52 +1000 Subject: [PATCH] Core: Fix server crashing if server name was too long (i.e. >= 47 chars) - thanks DramaticLove. Also restrict a number of max lengths for string values, such as server name to 64 chars. --- GUI/Window.cs | 2 +- MCGalaxy/Config/StringAttributes.cs | 30 ++++++++++++++++++++--------- MCGalaxy/Levels/Level.Fields.cs | 4 ++-- MCGalaxy/Server/Server.Fields.cs | 8 ++++---- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/GUI/Window.cs b/GUI/Window.cs index 8ff06993c..333a4c823 100644 --- a/GUI/Window.cs +++ b/GUI/Window.cs @@ -142,7 +142,7 @@ namespace MCGalaxy.Gui { Invoke(new VoidDelegate(SettingsUpdate)); } else { Text = Server.name + " - MCGalaxy " + Server.VersionString; - notifyIcon1.Text = ("MCGalaxy Server: " + Server.name).Truncate(64); + notifyIcon1.Text = ("MCGalaxy Server: " + Server.name).Truncate(63); } } diff --git a/MCGalaxy/Config/StringAttributes.cs b/MCGalaxy/Config/StringAttributes.cs index 5817ba989..6c8751c62 100644 --- a/MCGalaxy/Config/StringAttributes.cs +++ b/MCGalaxy/Config/StringAttributes.cs @@ -41,29 +41,33 @@ namespace MCGalaxy.Config { public sealed class ConfigStringAttribute : ConfigAttribute { - /// Whether the empty string is an allowed, or if is treated as the default value. + /// Whether an empty string is an allowed for the value, or if it is treated as the default value. public bool AllowEmpty; - /// Specifies the restricted set of characters (asides from alphanumeric characters) - /// that this field is allowed to have. + /// Specifies the restricted set of characters (asides from alphanumeric characters) + /// that the value is allowed to have. public string AllowedChars; + /// Maximum number of characters allowed in the value. 0 means no limit. + public int MaxLength = 0; + public ConfigStringAttribute(string name, string section, string desc, string defValue, - bool allowEmpty = false, string allowedChars = null) + bool allowEmpty = false, string allowedChars = null, int maxLength = 0) : base(name, section, desc, defValue) { AllowEmpty = allowEmpty; AllowedChars = allowedChars; + MaxLength = maxLength; } public override object Parse(string value) { if (value == "") { if (!AllowEmpty) { - Server.s.Log("Config key \"" + Name + "\" has no value, using default of " + DefaultValue); - return DefaultValue; + Server.s.Log("Config key \"" + Name + "\" has no value, using default of " + DefaultValue); + return DefaultValue; } return ""; } else if (AllowedChars == null) { - return value; + return Truncate(value); } foreach (char c in value) { @@ -71,11 +75,19 @@ namespace MCGalaxy.Config { continue; if (AllowedChars.IndexOf(c) == -1) { - Server.s.Log("Config key \"" + Name + "\" contains " + + Server.s.Log("Config key \"" + Name + "\" contains " + "a non-allowed character, using default of " + DefaultValue); - return DefaultValue; + return DefaultValue; } } + return Truncate(value); + } + + string Truncate(string value) { + if (MaxLength > 0 && value.Length > MaxLength) { + value = value.Substring(0, MaxLength); + Server.s.Log("Config key \"" + Name + "\" is too long, truncating to " + value); + } return value; } } diff --git a/MCGalaxy/Levels/Level.Fields.cs b/MCGalaxy/Levels/Level.Fields.cs index bf61c5da6..371f38498 100644 --- a/MCGalaxy/Levels/Level.Fields.cs +++ b/MCGalaxy/Levels/Level.Fields.cs @@ -27,7 +27,7 @@ namespace MCGalaxy { public sealed partial class Level : IDisposable { public string name; - [ConfigString("MOTD", "General", null, "ignore", true)] + [ConfigString("MOTD", "General", null, "ignore", true, null, 128)] public string motd = "ignore"; public byte rotx, roty; @@ -90,7 +90,7 @@ namespace MCGalaxy { // Environment settings [ConfigInt("Weather", "Env", null, 0, 0, 2)] public int Weather; - [ConfigString("Texture", "Env", null, "", true)] + [ConfigString("Texture", "Env", null, "", true, null, 64)] public string terrainUrl = ""; [ConfigString("TexturePack", "Env", null, "", true)] public string texturePackUrl = ""; diff --git a/MCGalaxy/Server/Server.Fields.cs b/MCGalaxy/Server/Server.Fields.cs index 9122e0d2b..91b459c0b 100644 --- a/MCGalaxy/Server/Server.Fields.cs +++ b/MCGalaxy/Server/Server.Fields.cs @@ -169,10 +169,10 @@ namespace MCGalaxy { public static bool TablistBots = false; [ConfigString("server-name", "General", null, - "[MCGalaxy] Default", false, "![]&:.,{}~-+()?_/\\' ")] + "[MCGalaxy] Default", false, "![]&:.,{}~-+()?_/\\' ", 64)] public static string name = "[MCGalaxy] Default"; [ConfigString("motd", "General", null, "Welcome", - false, "=![]&:.,{}~-+()?_/\\' ")] + false, "=![]&:.,{}~-+()?_/\\' ", 128)] public static string motd = "Welcome!"; [ConfigInt("max-players", "Server", null, 12, 1, 128)] public static int players = 12; @@ -372,9 +372,9 @@ namespace MCGalaxy { public static bool guestJoinNotify = true; [ConfigBool("guest-leave-notify", "Other", null, true)] public static bool guestLeaveNotify = true; - [ConfigString("default-texture-url", "General", null, "", true)] + [ConfigString("default-texture-url", "General", null, "", true, null, 64)] public static string defaultTerrainUrl = ""; - [ConfigString("default-texture-pack-url", "General", null, "", true)] + [ConfigString("default-texture-pack-url", "General", null, "", true, null, 64)] public static string defaultTextureUrl = ""; //hackrank stuff