From 83b030229ec1d91edcf00269eedfc9c9075b877e Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Sun, 21 Sep 2008 15:54:58 +0000 Subject: Mantis #2232 Thank you, idb, for a patch that fixes an overflow issue in casting string -> int for both engines, and adds tests! --- OpenSim/Region/ScriptEngine/Common/LSL_Types.cs | 33 ++++++++++++--- .../Common/Tests/LSL_TypesTestLSLInteger.cs | 8 ++++ OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs | 47 ++++++++++++---------- .../Shared/Tests/LSL_TypesTestLSLInteger.cs | 8 ++++ 4 files changed, 70 insertions(+), 26 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs index 8c24ae3..5bfe8bf 100644 --- a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs @@ -1269,8 +1269,7 @@ namespace OpenSim.Region.ScriptEngine.Common public static explicit operator LSLInteger(LSLString s) { - // double.Parse() used because s could be "123.9" for example. - return new LSLInteger(double.Parse(s.m_string)); + return new LSLInteger(s.m_string); } public static explicit operator LSLString(double d) @@ -1353,7 +1352,32 @@ namespace OpenSim.Region.ScriptEngine.Common public LSLInteger(string s) { - value = (int)double.Parse(s); + Regex r = new Regex("^[ ]*-?[0-9][0-9xX]?[0-9a-fA-F]*"); + Match m = r.Match(s); + string v = m.Groups[0].Value; + + if (v == String.Empty) + { + value = 0; + } + else + { + try + { + if (v.Contains("x") || v.Contains("X")) + { + value = int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber); + } + else + { + value = int.Parse(v, System.Globalization.NumberStyles.Integer); + } + } + catch (OverflowException oe) + { + value = -1; + } + } } #endregion @@ -1394,8 +1418,7 @@ namespace OpenSim.Region.ScriptEngine.Common static public explicit operator LSLInteger(string s) { - // double.Parse() used because s could be "123.9" for example. - return new LSLInteger(double.Parse(s)); + return new LSLInteger(s); } static public implicit operator LSLInteger(uint u) diff --git a/OpenSim/Region/ScriptEngine/Common/Tests/LSL_TypesTestLSLInteger.cs b/OpenSim/Region/ScriptEngine/Common/Tests/LSL_TypesTestLSLInteger.cs index d2839db..a769608 100644 --- a/OpenSim/Region/ScriptEngine/Common/Tests/LSL_TypesTestLSLInteger.cs +++ b/OpenSim/Region/ScriptEngine/Common/Tests/LSL_TypesTestLSLInteger.cs @@ -62,6 +62,14 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests m_stringIntSet.Add("123.9", 123); m_stringIntSet.Add("999999999", 999999999); m_stringIntSet.Add("-99999999", -99999999); + m_stringIntSet.Add("", 0); + m_stringIntSet.Add("aa", 0); + m_stringIntSet.Add("42", 42); + m_stringIntSet.Add("42 is the answer", 42); + m_stringIntSet.Add(" 42", 42); + m_stringIntSet.Add("42,123,456", 42); + m_stringIntSet.Add("0xff", 255); + m_stringIntSet.Add("12345678900000", -1); } /// diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs index 91cef5e..d107d90 100644 --- a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs @@ -1353,16 +1353,7 @@ namespace OpenSim.Region.ScriptEngine.Shared public static explicit operator LSLInteger(LSLString s) { - Regex r = new Regex("^[ ]*-?[0-9][0-9xX]?[0-9a-fA-F]*"); - Match m = r.Match(s); - string v = m.Groups[0].Value; - - if (v == String.Empty) - v = "0"; - - if (v.Contains("x") || v.Contains("X")) - return new LSLInteger(int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber)); - return new LSLInteger(int.Parse(v, System.Globalization.NumberStyles.Integer)); + return new LSLInteger(s.m_string); } public static explicit operator LSLString(double d) @@ -1445,7 +1436,30 @@ namespace OpenSim.Region.ScriptEngine.Shared public LSLInteger(string s) { - value = (int)double.Parse(s); + Regex r = new Regex("^[ ]*-?[0-9][0-9xX]?[0-9a-fA-F]*"); + Match m = r.Match(s); + string v = m.Groups[0].Value; + + if (v == String.Empty) + { + value = 0; + } + else + { + try + { + if (v.Contains("x") || v.Contains("X")) + value = int.Parse(v.Substring(2), + System.Globalization.NumberStyles.HexNumber); + else + value = int.Parse(v, + System.Globalization.NumberStyles.Integer); + } + catch (OverflowException oe) + { + value = -1; + } + } } #endregion @@ -1486,16 +1500,7 @@ namespace OpenSim.Region.ScriptEngine.Shared static public explicit operator LSLInteger(string s) { - Regex r = new Regex("^[ ]*-?[0-9][0-9xX]?[0-9a-fA-F]*"); - Match m = r.Match(s); - string v = m.Groups[0].Value; - - if (v == String.Empty) - v = "0"; - - if (v.Contains("x") || v.Contains("X")) - return new LSLInteger(int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber)); - return new LSLInteger(int.Parse(v, System.Globalization.NumberStyles.Integer)); + return new LSLInteger(s); } static public implicit operator LSLInteger(uint u) diff --git a/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLInteger.cs b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLInteger.cs index f826c00..957a3b6 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLInteger.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLInteger.cs @@ -62,6 +62,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests m_stringIntSet.Add("123.9", 123); m_stringIntSet.Add("999999999", 999999999); m_stringIntSet.Add("-99999999", -99999999); + m_stringIntSet.Add("", 0); + m_stringIntSet.Add("aa", 0); + m_stringIntSet.Add("42", 42); + m_stringIntSet.Add("42 is the answer", 42); + m_stringIntSet.Add(" 42", 42); + m_stringIntSet.Add("42,123,456", 42); + m_stringIntSet.Add("0xff", 255); + m_stringIntSet.Add("12345678900000", -1); } /// -- cgit v1.1